Missing ";" before 'namespace' and ";" before 'using' - c++

So I am working on a program that is due tomorrow and for some reason I keep getting this 2 errors, if I click on the first one it takes me to the iostream file and right before the _STD_BEGIN it wants me to put ";" but if I do that it messes up the file in the library so I am pretty sure I do not have to do that, the second error is in my main.cpp and it points to using namespace std; and it wants me to put a ";" before it =, if I do so the error disappears and it keeps pointing at the iostream error....
I have no idea what to do and my deadline is tomorrow.
This is my main.cpp include section with the modification to using namespace std
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
#include "Package.h"
;using namespace std;

Look for a class or struct definition in Package.h that's missing its semicolon. ie.
class act
{
// yadda
} // no semicolon here
Then add the missing semicolon.

When you get a "missing ;type error on a line that follows closeley behind a bunch of#includestatements, the likely culprit is a missing;` in one of the header files. To find out which, start at the last include file, Package.h. You'll surely find a missing semicolon there. It's probably missing after a class declaration, as if you had written:
class Foo
{
}
instead of
class Foo
{
};

Related

C++ error code C2065: '<class name>' undeclared identifier, even though it should be declared in another .h-file

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"

Errors in main.cpp after delcaration of .h files

here's my problem:
#include<iostream>
#include<fstream>
#include<memory>
#include<stdio.h>
#include<sstream>
#include<utility>
#include<algorithm>
#include<vector>
#include "classes.h"
#include "firstfunction.h"
#include "secondfunction.h"
#include "third function.h"
using namespace std;
int main(){
// do cool stuff
}
but when i compile I get the errors (all at the same line, the one of using namespace)
error: only constructor take member initializer
error: expected identified before 'using'
error: expected '{' before using
if I try to remove "using namespace std" I still get the same with 'int' instead of using.
the rest of the code seems to compile without errors! do you have any ideas?
Why don't you try to remove the space between in "third function.h"? I don't know, but maybe changing the name of the file to "thirdfunction.h" or to "third_function.h" would work. C++ is a really bad guy when you have to deal with blank spaces. Also, you should check the implementations inside your .h files (from "classes.h" to "third function.h"), cause the error could be in one of them. I should look first if it works changing the name of "third function.h" and then deep-looking into your other .h files...

The ordering of namespace reference and includes affects compilation results

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.

No operator== match for std::string?

I got a strange problem with my program. So in the header I got something like this:
#ifndef SET1_INCLUDED
#define SET1_INCLUDED
#include <iostream>
using namespace std;
typedef std::string ItemType;
class Set1{
public:
------some public constructor and method in here-------
private:
ItemType setMember[100];
}
in 1 part of my function in the Set1.cpp file I got something like this :
if (setMember[i] == "foo") {exist = true;}
In this case, I got an error message that says "no operator found which takes a left-hand operand of type 'ItemType' ". However, if I change the std::string in the typedef into int or unsigned long, and change "foo" to some random number, the code works perfectly. Any suggestion? thx
You are missing the header file <string>, which means that you don't have all of the global operator == definitions visible in your program. This is likely the case of your problem.
To fix this, try adding in this line:
#include <string>
Hope this helps!
You need to include string header file to bring relative types and operator to scope.
#include <string>
Note:
It's bad coding practice to pull in everything from std namespace in header file.
//using namespace std;

Compile errors when trying to use list in C++

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!