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.)
Related
I have two files: DateTime.h and DateTime.cpp which are shown below:
DateTime.h
class DateTime
{
public:
static string getCurrentTimeStamp();
};
DateTime.cpp
#include "stdafx.h"
#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
My compiler (Visual Studio 2012) is spitting out errors the moment I have the function getCurrentTimeStamp() return an std::string object. The errors all point to syntactic problems but none are clear. Does anyone understand why this may be the case?
Update: Here are (some of) the errors.
Error 6 error C2064: term does not evaluate to a function taking 0
arguments c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 21 1 ConsoleApplication1
Error 1 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 7 error C2146: syntax error : missing ';' before identifier
'getCurrentTimeStamp' c:\users\anthony\documents\code\consoleapplication1\datetime.h 5 1 ConsoleApplication1
Error 5 error C2371: 'DateTime::getCurrentTimeStamp' : redefinition;
different basic
types c:\users\anthony\documents\code\consoleapplication1\datetime.cpp 10 1 ConsoleApplication1
When trying to diagnose an issue with a header file, especially a simple one like this, step 1 is to try and see what the compiler sees.
#include is a pre-processor directive, so the compiler doesn't see it, instead the compiler sees the pre-processed output of the file that you're trying to include.
So your code would look something like this:
#include "stdafx.h"
//#include "DateTime.h"
class DateTime
{
public:
static string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
http://rextester.com/MODV66772
When I try and compile this on RexTester's online Visual Studio, I get very different errors, telling me that your stdafx.h isn't empty.
If I modify the code a little:
//#include "stdafx.h"
//#include "DateTime.h"
#include <string>
class DateTime
{
public:
static std::string getCurrentTimeStamp();
};
//#include "DateTime.h"
#include <ctime>
#include <chrono>
#include <iostream>
#include <string>
using namespace std;
string DateTime::getCurrentTimeStamp()
{
return "";
}
This now compiles without the errors/warnings you are reporting: http://rextester.com/PXE62490
The changes:
include in the header file, since the header depends on it,
use std::string instead of string,
The C++ compiler is a single-pass compiler, so the header file can't know that you are intending to do using namespace std later, and even if it did, it's a terrible practice because the std namespace is densely populated.
If you absolutely can't do with typing std:: all over the place, try using the names you need, e.g.
using std::string; // string no-longer needs to be std::string
this is probably an include problem, i get these errors all over the code, and not only for string identifier for example error C2146: syntax error : missing ';' before identifier 'getName' and error C2146: syntax error : missing ';' before identifier 'name'
here's an example class:
#include "stdafx.h"
class participant
{
public:
participant(int id, string name);
~participant(void);
int getId();
string getName();
private:
int id;
string name;
};
here's my stdafx.h file:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
using namespace std;
#define no_such_appointment_error 20;
#define conflicting_appointments_error 21;
#define noSuchDayError 22;
#define incorrectAppointmentError 23;
So I compiled your code as-posted without your custom header files and it worked just fine. Based on that, I am going to wager that you have a problem in one of these header files:
#include "day.h"
#include "appointment.h"
#include "extendedAppointment.h"
#include "participant.h"
#include "calendar.h"
It could be a macro, a class/struct not terminated with a semi-colon, etc. Check those out.
Lastly, a few of tangential issues:
First, using a namespace in a header file is a terrible idea. Any file that includes your header now has using namespace std; in it (this is bad). You probably don't want to include that many header files in every file that includes stdafx.h.
Secondly, once you remove that then string immediately becomes undefined (use std::string instead).
Last, why are your #defines ended with semi-colons? No need for that.
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> ?
I'm stuck! I have this very simple test code and I can't get it to compile! I have used the same code many times before but now it won't work!
I have this simple program
#include <vector>
#include <iostream>
#include "Rswap.h"
using namespace std;
int main(){
Rswap test();
cin.get();
return 0;}
And then the rswap.cpp...
#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
V.push_back(9);
};
And then the rswap.h...
#ifndef Rswap_h
#define Rswap_h
class Rswap{
public:
vector<int>V;
Rswap();
};
#endif
I'm using Visual studio 2008. Is there something wrong that is obvious and I'm missing or what could it be! As I said I have used this snippet on several differnet occassions and I can't find any difference between this and those that work...
And i have tried both
vector < int > V; and vector <int> V; without any luck
I have stared at this blindly now for some while so I thought it's better to ask here!
rswap.h(7) : error C2143: syntax error : missing ';' before '<'
rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
rswap.h(7) : error C2238: unexpected token(s) preceding ';'
At the point you #include "Rswap.h", you haven't declared using namespace std; yet, so the reference to vector in Rswap.h must be qualified with a namespace. Simply declare the vector with a namespace prefix.
class Rswap{
public:
std::vector<int>V;
Rswap();
};
Also, I suggest you #include <vector> from Rswap.h rather than relying on whoever uses Rswap.h to get the #include order right.
It should be
std::vector<int>V;
Ah, your using namespace std comes too late - it should be inserted BEFORE the rswap.h include. In any case, it's MUCH better to write std::vector instead.
You need to #include <vector> in rswap.h.
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!