I'm trying to use list in c++, but I get the following error:
1>error C2143: syntax error : missing ';' before '<'
1>error C4430: missing type specifier int assumed. Note: C++ does not support default-int
1>error C2238: unexpected token(s) preceding ';'
With the following code:
#pragma once
#include "Includes.h"
class Polygon
{
public:
Polygon(void);
~Polygon(void);
void addVertice(hgeVector v);
void renderPolygon();
list<hgeVector> vertices;
};
Includes.h:
#ifndef INCLUDES
#define INCLUDES
#define safe_delete(d) if(d) { delete d; d=0; }
#define PI 3.14159
#include <stdio.h>
#include <list>
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
#include "Car.h"
#include "HelperFunctions.h"
#include "config.h"
#include "Polygon.h"
using namespace std;
#endif
Just some general comments...
#define PI 3.14159
Please use M_PI in math.h, which is 3.141592653589793238462643.
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
You should use forward slashes / here, and remove the leading \ before the include.
using namespace std;
Avoid this in header file. This will pollute all other users' global namespace. (Therefore, you should use std::list<hgeVector> vertices; in Polygon.h.)
The issue could be that the line list<hgeVector> vertices is being processed before using namespace std;, and so your compiler does not know what a list (without the std:: namespace qualifier) is. It's not clear to me in exactly what order these statements get processed since your two files include each other, and I don't know precisely how the non-standard #pragma once will handle this.
In any case, try qualifying list<hgeVector> as std::list<hgeVector>
Edit: Assuming #pragma once works just like include guards, then this problem will occur if some other file inlcudes includes.h, but not if some other file includes Polygon.h. If another file includes includes.h, what happens is that includes.h reaches #include <Polygon.h>, and the compiler starts processing Polygon.h. But then when #include <includes.h> is reached inside Polygon.h, nothing is effectively included since the INCLUDES guard is already defined, so you don't get the using namespace std; line before the compiler continues processing the rest of Polygons.h.
In general, try to avoid circular inclusion, and prefer forward-declarations.
I think you have circular "includes". You are including Includes.h in Polygon.h and including Polygon.h in Includes.h.
class template need a full type declaration to instantiate itself. Make sure you have included the header file where hgeVector is declared.
BTW, you have 'using namespace std‘ in your header - this is not a good practice. It will introduce unnecessary names to the current namespace.
Make sure hgeVector is defined.
You may have redefined list somewhere. Try using std::list.
Try something very simple like std::list<int>.
The answer (as Tyler McHenry pointed out) was circular inclusion!
After sorting out my includes I ended up with a compiled code like this (even without std:: infront of list:
#pragma once
#include <list>
#include "D:\Programmering\haffes\hge181\include\hge.h"
#include "D:\Programmering\haffes\hge181\include\hgevector.h"
using namespace std;
using namespace std;
class MyPolygon
{
public:
MyPolygon(void);
~MyPolygon(void);
void addVertice(hgeVector v);
void renderPolygon();
void setHotSpot(hgeVector v);
void translate(hgeVector v);
private:
list<hgeVector> vertices;
hgeVector hotSpot;
bool hotSpotUndef;
};
Thanks a bunch for all the fast and good answers!
Related
i have a problem with following code:
Generator.h:
#pragma once
class Generator
{
public:
friend class BagObject;
Generator(void);
~Generator(void);
...
void generator(int);
private:
BagObject *object;
vector<BagObject> data; //Error c4430
};
and this is a error:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
there is 6 more errors but i believe that they should disappeared after solving this problem.
this is the cpp file. I couldn't paste it on the first time.
Generator.cpp
#include "stdafx.h"
#include "Generator.h"
#include "BagObject.h"
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
Generator::Generator(void)
{
srand(time(NULL));
}
Generator::~Generator(void)
{
data.clear();
}
void Generator::generator(int ld)
{
for (int i = 0; i<ld; i++)
{
object = new BagObject(rand(),rand(),i);
data.push_back(object);
}
}
int main()
{
Generator *g = new Generator;
g->generator(10);
return 0;
}
Either you forgot to include header
#include <vector>
or forgot to write directive
using namespace std;
In any case it would be better to write
#include <vector>
//...
std::vector<BagObject> data;
^^^^^
You have to include the header <vector> in all headers where there is a reference to std::vector.
Other answers are correct, but cryptic. In plain English, your header does not know about BagObject class. You included BagObject.h in the .cpp, but you should have included it in the .h.
It also does not know about vector for the same reason.
I am guessing, you were under impression that .cpp had to use #include, but .h did not. This is a common misunderstanding of beginners in C++. Headers need to include all referenced class declarations, hence you need to elevate your includes from .cpp into your .h.
Move two mentioned includes into the header and it will work.
vector may not be instantiated with an incomplete type. In order to have vector<BagObject> data; in the header, the header must also have #include "BagObject.h".
(This is in addition to the changes recommended in Vlad's answer)
I know your problem is solved but in My case the same error was caused due to cyclic includes (i.e. I had accidentally included the .h file in one of the .h file included in it)
TextureManager.h (The file with the error)
// This is TextureManager.h
#pragma once
#include "Game.h"
#include "GameObject.h"
/*
texture manager class
*/
GameObject.h
// This is GameObject.h
#pragma once
#include "game.h"
#include "TexutureManager.h" // Accidental
/*
*/
I thought it may be worth noting this is also one of the ways to get this error.
I have a multifile program, and I can't figure out why my program says that "Customers" (in the registerNewUser() function) is an undeclared identifier.
proc.h
#ifndef PROC_H
#define PROC_H
#include <iostream>
#include "const.h"
#include "customers.h"
#include <fstream>
using namespace std;
void registerNewUser(Customers cBase); // Add new user.
#endif // !PROC_H
I have included the header file (customers.h) with the Customers class also.
customers.h
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
#include <iostream>
#include "const.h"
#include "proc.h"
#include "customer.h"
using namespace std;
class Customers {
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif // !CUSTOMERS_H
Can anyone see what's wrong?
You have a circular include. customers.h includes proc.h so basiacally
void registerNewUser(Customers cBase);
Will get added to customers.h before the compiler has seen what a Customer is. It looks like you should just be able to remove the #include "proc.h" in customers.h and it should compile.
As stated in the comments above you should never use using namespace std; in a header file as anything that includes it now has the entire std namespace exposed. You should also get in the habit of only using it in the most narrow scope you can or drop it completely. For further reading on the use of using namespace std; see Why is “using namespace std” in C++ considered bad practice?
Basically including "customers.h" in "customers.h" wouldn't be a problem here, since you have a guard (plus point for that). Nevertheless it is not very nice.
As NathanOliver said it COULD be a problem with the order of the includes but it doesn't have to. If you include proc.h first everything is fine. If you include customers first, the compiler includes proc.h before he sees the customer class. proc then wont include customers.h (since its guard prevents it). Then he will find your function not knowing what "Customer" means. So depending on the include order of your header files it will or will not work.
If you want a hint: I normally first only include the necessary files for a forward declaration, then do a forward declaration. Then I include the files necessary files for the definition of the class (These will already know that the class exists). The complete class declaration (with member function declaration) follows. If you do it like this you can avoid many mistakes. In your case:
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
class Customers;
#include "proc.h"
#include "ListTool2B.H"
using namespace std;
class Customers
{
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif
This is probably a duplicate: you have proc.h including customers.h and customers.h including proc.h this will cause a circular reference, and looks like proc.h included in customers is not necessary, so you could try simply to delete this line:
#include "proc.h"
I always think we should use namespace after includes, for example, we should use:
#include <vector>
using std::vector;
instead of
using std::vector;
#include <vector>
but I find the second one is OK, what confuses me is that different ordering of them causes different results.See following simple example: here are two header files and one c++ file.
// test1.h
#include <vector>
using std::vector;
#include "test2.h"
// test2.h
vector<int> v;
// test.cpp
#include "test1.h"
int main()
{
return 0;
}
Although test2.h doesn't include vector, but it compiles OK all above.
What's strange is when I swap the order of using std::vector; and #include , there happens a compile error like this:
error C2143: syntax error : missing ';' before '<'
error C2501: 'vector' : missing storage-class or type specifiers
error C2143: syntax error : missing ';' before '<'
error C2874: using-declaration causes a multiple declaration of 'vector'
I do not understand the internals, does the order of namespace and includes really matter? why so this?
It's usually a bad idea to dump names into the global namespace, where they might clash with user-declared names - especially in a header, where you force the pollution on all users of that header. Having said that:
I find the second one is OK
Only because something has already included <vector> or otherwise declared std::vector. As you can see from your later example, you get errors otherwise.
I do not understand the internals, does the order of namespace and includes really matter?
Yes, a using-declaration can only refer to names that have already been declared.
why so this?
Because that's how the language works. You can't usually use a name that hasn't been declared.
// test2.h
vector<int> v;
Header files should be self-sufficient. Writing test2.h like this requires that the user both #include <vector> and add a using declaration so that vector can be written without qualifying its name. That means the header isn't self-sufficient, because you can't simply #include it without doing a couple of other things first.
You also should not be defining variables in a header file. In a header file you should only be declaring extern variables.
#ifndef TEST2_H
#define TEST2_H
#include <vector>
extern std::vector<int> v;
#endif
Here I've also added #ifndef/#define guards so the header file can be included multiple times without error. I've also used the fully qualified std::vector with no using: it's legal, but bad practice, to put using declarations in header files.
This:
using std::vector;
#include <vector>
is not OK, unless <vector> has already been included by that file.
The important thing here is that #include does textual substituion. It simply works as if it pasted the contents of the included file over the #include directive. Header files are not compiled by themselves, they only form textual parts of the source file being compiled.
So your example works because by the time test2.h is included, the source file being compiled (test.cpp) already contains #include <vector> and using std::vector; from test1.h. And that's also why it breaks when you swap - test2.h gets included before the using declaration, so unqualified vector does not exist at the point it's used.
The preprocessor directive #include is a text replacement operation. It will read the included file and replace the #include by its contents. If you think in terms of what the operation is doing the answer should be clear.
After preprocessing main.cpp becomes:
// start test1.h
#include <vector>
using std::vector;
// start test2.h
vector<int> v;
// end test2.h
// end test1.h
int main()
{
return 0;
}
If you change the order of the using directive with respect to the inclusion of the header you will get (removing comments):
#include <vector>
vector<int> v;
using std::vector;
//...
At the point of definition of v, the using declaration has not been seen, and the compiler does not know what vector means and thus the error message.
Now back to the original assumption:
using std::vector;
#include <vector>
but I find the second one is OK
No, it is not ok. Depending on what other includes you have before hand, it might compile, but you cannot provide a using declaration before the compiler has seen the element being declared. If the two lines above compile, it means that somewhere before the first line, the expansion of the includes already brought a declaration of std::vector into this translation unit. Try to compile that by itself in a single .cpp and you will see a couple of errors.
I want to make some STL sets but when I do it tells me
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
Meaning I have not included the proper header define (#define <set>) but I have. Are sets just under a different location? The source looks like this
set<int> playerlist;
The header files are...
#include <iostream>
//#include "winsock2.h"
#include "Ws2tcpip.h"
#include <list>
//#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <map>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#include <set>
What you need is
#include <set>
and not
#define <set>
also before using the set write the following line before your main function
using namespace std;
By
#define <set>
I hope you mean
#include <set>
?
You also need to indicate somehow that you want the set from the std namespace. You can do that either by prefixing set with std:: everywhere that you refer to it:
set<int> playerlist;
or by using a using declaration to tell the compiler that whenever you refer to set, you mean std::set:
using std::set;
(It's also possible to write a catch-all using namespace std; declaration that says that you always want to use everything from the std namespace, but that's normally considered bad practice. I mention it only because, despite being bad practice, it's fairly common, so you may well come across code that does it.)
This is weird. I created a vector just fine in one class but can't create it in another class. He's a representation of what I have:
main.h
#include <Windows.h>
#include <ShellAPI.h>
#include <vector>
#include <string>
#include <iostream>
#include "taco.h"
class MyClass
{
public:
int someint;
vector<int> myOrder;
};
taco.h
#include <vector>
class OtherClass
{
public:
vector<int> otherOrder;
};
And I get compile errors regarding the vector declaration in taco.h:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
What am I missing here? I can uncomment out that second vector declaration and it compiles fine.
Try:
std::vector<int> otherOrder;
vector is part of the std namespace. This means that whenever you use a vector in a header file, you should include the std:: prefix.
The reason that you can sometimes get away with forgetting it is that some included files may have using namespace std; in them, allowing you to leave off the prefix. However, you should avoid the using keyword in header files, for it will pollute the namespace of any files that include it.
For a more detailed explanation of the dangers of using namespace ..., see this thread.
Try std::vector<int>. You're supposed to use the namespace --- I'm assuming you have
using namespace std;
in the main.h someplace. There's a lot of talk on SO as to why using using is bad practice ; I'd recommend that you check it out.
All C++ standard library objects live in the std namespace. Try
class MyClass
{
public:
int someint;
std::vector<int> myOrder;
// ^^^^^
};
std::vector<int> ?