开发者

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

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. Co开发者_JAVA技巧uld 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 


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


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;
}


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[][];


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{
    ...
  };


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.


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...)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜