error: expected `;' before '{' token - What is the cause?

旧时模样 提交于 2020-01-24 20:59:28

问题


Here is my implementation file:

using namespace std;

#include <iostream>
#include <iomanip>
#include <string>
#include <stack>  //line 5
#include "proj05.canvas.h"

//----------------Constructor----------------//

Canvas::Canvas() //line 10
{
  Title = "";
  Nrow = 0;
  Ncol = 0;
  image[][100];  // line 15
  position.r = 0;
  position.c = 0;
}

//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color) 
{
  cout << "Paint to be implemented" << endl;
}

The errors I'm getting are these:

proj05.canvas.cpp: In function 'std::istream& operator>>(std::istream&, 
    Canvas&)':
proj05.canvas.cpp:11: error: expected `;' before '{' token
proj05.canvas.cpp:22: error: a function-definition is not 
    allowed here before '{' token
proj05.canvas.cpp:24: error: expected `}' at end of input
proj05.canvas.cpp:24: error: expected `}' at end of input

These seem like simple syntax errors, but I am not sure what's wrong. Could someone decode these for me? I'd really appreciate it, thanks for your time!


EDIT

Here is the definition of Canvas in my .h file:

#ifndef CANVAS_H 
#define CANVAS_H 

#include <iostream>
#include <iomanip>
#include <string> 
#include <stack> 

class Canvas
{
  public: 
      Canvas(); void Paint(int R, int C, char Color); 
       const int Nrow; 
       const int Ncol; 
       string Title; 
       int image[][100]; 
       stack<int> path; 
       struct PixelCoordinates 
       {  
         unsigned int r; 
         unsigned int c;
       } position; 
}; 

#endif 

回答1:


"proj05.canvas.h" i bet the problem is there. may be no ; after class def




回答2:


You must use initializer list to initialize const-members

Try this:

#include <iostream>
#include <iomanip>
#include <string>
#include <stack>  //line 5
#include "proj05.canvas.h"

using namespace std;

//----------------Constructor----------------//

Canvas::Canvas():Nrow(),Ncol() // Initializer list
{
  Title = "";
  //initialize image[][] correctly, your way is syntactically incorrect
  position.r = 0; //correction here
  position.c = 0; // and here
}

//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
   cout << "Paint to be implemented" << endl;
}



回答3:


Few things:

1

PixelCoordinates.r = 0;
PixelCoordinates.c = 0;

should be:

position.r = 0;
position.c = 0;

2

image has already been declared. What is this:

image[][];



回答4:


It sounds like you forgot to put a semicolon after your class definition. Look in "proj05.canvas.h". You should see something like:

  class Canvas{
    ...
  };



回答5:


One thing that catches my eye as wrong/weird is image[][]. That does not really do anything. Also, I do not believe you can assign to constant member outside of a ctor list.

Finally, your assignment to PixelCoordinates is completely in error. You've created a local struct definition, but have not made a member that uses it, therefore you cannot assign anything at all to it - especially the struct's title. That would really confuse a compiler.




回答6:


Yikes.

(Not an answer to your specific problem, but...)

You should also remove the

using std;

That has no business in a .h file. I am going to guess the oddly formatted .h file may be a problem. It is legal for a filesystem of course, but it could be that. Also ensure you have the ending semicolon on the class.

You need to have both dimensions filled in for the array you have (probably a horrible design to use that int he class anyway...)




回答7:


Whatever the reason for other errors is, the memeber definition int image[][100] is illegal. Non-static data members of the class cannot be declared with incomplete types. All dimensions of an array must be specified explicitly.



来源:https://stackoverflow.com/questions/2646592/error-expected-before-token-what-is-the-cause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!