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

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

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

Related

Why won't C++ let me use a string as a data member in a class?

So I have the following code in a header file named Classes.h:
#ifndef CLASSESS_H
#define CLASSESS_H
class PalindromeCheck
{
private:
string strToCheck;
string copy;
public:
PalindromeCheck(string testSubject) : strToCheck(testSubject) {} //Constructor
void Check()
{
copy = strToCheck; //Copy strToCheck into copy so that once strToCheck has been reversed, it has something to be checked against.
reverse(strToCheck.begin(), strToCheck.end()); //Reverse the string so that it can be checked to see if it is a palindrome.
if (strToCheck == copy)
{
cout << "The string is a palindrome" << endl;
return;
}
else
{
cout << "The string is not a palindrome" << endl;
return;
}
}
};
#endif
And now I have the following code in a source file:
#include <iostream>
#include <string>
#include <algorithm>
#include "Classes.h"
using namespace std;
int main()
{
PalindromeCheck firstCheck("ATOYOTA");
firstCheck.Check();
return 0;
}
When I compiled this code using the Visual C++ Compiler, I got a ton of errors messages that all stemmed from these first four:
'strToCheck': unknown override specifier
missing type specifier - int assumed.
'copy': unknown override specifier
missing type specifier - int assumed.
I tried adding in #include <string> into the header file and recompiled it but it did absolutely nothing. This confuses me because I thought I could use a string as a datatype, but apparently not in a class? It would be great if someone could help me out because I don't know why my code isn't working.
You need to #include <string> in the class header itself.
You also need to either use the std:: namespace (preferable) or also add using namespace std to that header as well (which I strongly discourage).

'vector' was not declared in this scope

I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors: namespace std, spelling, include vector, etc. Below is the abbreviated code for my video.h file.
#include <iostream>
#include <string>
#include <new>
#include <vector>
using namespace std;
class Video
{
public:
Video(string, string, string, float, int);
vector<Video*> video_ptrs;
void print();
};
And here is the code for my main.cpp
#include "video.h"
using namespace std;
int main()
{
...
Video* temp_here = new Video(title, url, comment, length, rating);
video_ptrs.push_back(temp_here);
return 0;
}
The error that returns says, "'video_ptrs' was not declared in this scope." Thank you ahead of time for any help given.
video_ptrs is a member of Video, call it with the object you just created:
Video* temp_here = new Video(title, url, comment, length, rating);
temp_here->video_ptrs.push_back(temp_here);
This adds a pointer temp_here to the vector of that same object though, I'm not sure if this is intended.
video_ptrs is indeed not declared in main. You shall use
temp_here->video_ptrs...

Errors with declaring/defining and vectors

I'm working on some code and I've come across a few errors to do with defining/declaring and expecting a type specifier where my class name is?
I was wondering if anyone can simply explain to me where I've gone wrong and how to resolve these issues?
I've commented out the errors on each lines, Boot and Shoe and Footwear are classes in a header file Footwear.h
#include <iostream>
#include <string>
#include <vector>
#include "typedefs.h"
#include "Footwear.h"
int main(int argc, const char * argv[])
{
vector<Footwear*> collection; // vector and Footwear are undefined
Footwear* f; //f undefined
f = new Boot("Timbaland",10); //expected a type specifier
collection.push_back(f); //collection undefined
f = new Shoe("Brogue",5); //expected a type specifier
collection.push_back(f);
for (i = 0; i < collection.size(); i++) //i undefined
collection[i]>toString(); //toString undefined
return 0;
}
Use: std::vector
That may clear all the warnings. You've included but you still need to prefix the std namespace.
In your for loop, you must declare for(int i = 0....) you may want to use unsigned int since collection.size() should never be < 0.

Confusion about constructors - expected a ';'

Instead of putting my class in the same file as my main function, I'm trying to use a #include. Though, when I do this, I get an error for my constructor. This is my input.cpp file:
#ifndef input
#define input
using namespace std;
#include <string>
#include <iostream>
class input
{
public:
input(int sent)
{
s = sent;
}
void read();
void store(string s);
private:
int s;
};
#endif
This is my main function:
#include <iostream>
#include <string>
using namespace std;
#include "input.cpp"
int main()
{
cout<<"Hello, please enter your input"<<endl;
string sent;
getline(cin, sent);
cout<<sent;
input1 *newinput = new input1("hello");
system("pause");
return 0;
}
The error I'm getting is
"intelliSense expected a ';'"
in the body of my constructor. Though, when I copy / paste the class directly into my main.cpp file, the error goes away. Any idea on what is causing this?
Do no use using namespace in headers
You have input as macro constant and name of class is the same. I afraid it's the root your problem.
Prefer to use constructor initialization lists input(int sent) : s(sent) {}
UPDT
you may need constructor able to accept string as parameter input(const std::string& str1) : str(str1) {} where str is class member to handle string data.
You defined the constructor as having one parameter of type int
input(int sent)
{
s = sent;
}
but try to call it passing as an argument a string literal
input *newinput = new input("hello");
The string literal that has type const char[6] can not be implicitly converted to type int and the class has no other constructor that accepts character arrays as arguments.
EDIT: You changed you original post several times so it is not clear now whether using name input1 in stateent
input1 *newinput = new input1("hello");
is a typo or it is some other type.
Also you have a macro definition with the same name as the class name
#ifndef input
#define input
Change either the macro name or the class name.

Class does not name a type error

I have the following code in a project:
#ifndef MAP_H
#define MAP_H
#include <string>
#include "MapCell.h"
using namespace std;
class Map{
public:
Map();
Map(int, int);
Map(string);
virtual ~Map();
private:
int grid_size;
MapCell * grid;
};
#endif
When I go to compile, I get the error "error: 'MapCell' does not name a type" yet when I comment out MapCell * grid and run this next block of code from main:
#include <iostream>
#include <cstdlib>
#include "MapCell.h"
using namespace std;
int main() {
MapCell * test_var;
test_var = new MapCell();
delete test_var;
cout << "Press enter to end process...";
cin.get();
cin.get();
return 0;
}
everything works just fine. I know my MapCell.h and .cpp files are in the right places and I'm guessing the compiler can see them since it works from main. I read around some other questions and most answers seem to point to either syntax errors or forward declarations which don't really fit here (unless I'm overlooking something)
Any ideas on what's going on here?
chris and greatwolf led me to the solution that fixed the problem. I needed to forward declare my MapCell class for the compiler to be able to link to the whole class.