compiler reports: error: expected unqualified-id before ‘using’ - c++

I have two files, homework1.cc and homework1.h.
The top of homework1.cc is
#include <iostream>
#include <fstream>
#include <cstring>
#include <GL/glut.h>
#include "homework1.h"
using namespace std;
and the top of in homework1.h is
#ifndef _rt_H
#define _rt_H
#include <cmath>
#include <vector>
#include <list>
#include <GL/glut.h>
using namespace std;
This is where I use list in homework1.h
class surTriangle
{
float x;
float y;
float z;
list <int> surTriangles;
bool compareVertex(Vector3f anotherVec)
{
if(anotherVec[0] == x && anotherVec[1] == y && anotherVec[2] == z)
return true;
return false;
}
}
When I attempt to compile homework.cc, the compiler reports
homework1.cc:8: error: expected unqualified-id before ‘using’
However when I delete #include <list> or the third piece of code above it compiles successfully.
Would you please help me to find out what's the problem is?

You need a semicolon after class declaration. (the last line of the class fragment of homework1.h). Otherwise using is interpreted as identifier as in
class X {
} x;

You forgot ; at the end of your class definition.
When debugging parse errors, it's useful to consider how the preprocessor works; in your mind (or with gcc -E if you want to actually view it properly) imagine what the code looks like after #include directives have been resolved.
You'd then see that the error falls directly after your class definition, which narrows things down tremendously. The error says that using is "unexpected", which typically indicates an issue terminating/concluding the previous statement. This leads to spotting the missing semicolon, which is a classic typo.

Related

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

N does not name a type

// q.h file
#ifndef __Q_H__
#define __Q_H__
using namespace std;
#include "n.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class Q
{
public:
Q();
private:
N* beginning; //error N does not name a type
N* end; //error N does not name a type
int count;
};
#endif // end of file
// q.cpp file
#include "q.h"
#include "n.h"
#include <iostream>
using namespace std;
#include<string>
Q::Q()
{
beginning = NULL;
end = NULL;
}
// n.h file
#ifndef _N_H__
#define _N_H__
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "q.h"
class N
{
public:
N(int);
// next is a pointer object of type N
N* next;
// memeber for Node class
int value;
};
#endif
// n.cpp file
#include<string>
#include "q.h"
#include "n.h"
N::N(int v)
{
value = v;
}
I get an error at the following lines below. I tried changing user namespace std; to the first line of each file but that still does not work. I've also tried changing the order of q.h and n.h in which they are presented but still nothing.
N* beginning;
N* end;
Also I know one great solution is by using "forward declare" but this is part of a test file that i given to me by someone so I cannot do a forward declaration of a class.
So if anyone can please just take a look and see how I can fix this.
Thanks.
You have a circular reference (q.h includes n.h and n.h includes q.h). This error occurs because the compiler is including (maybe because the main program) q.h before than n.h. Thus, q.h need to include an empty class declaration as follows to Q knows about N.
...
#include <string>
class N;
class Q
{
...
If you can't edit those files, you still can add the empty class declaration anywhere before the "Q" class declaration (e.g. before including q.h in your main program). However, the class N don't need anything from Q, so you don't need to include q.h in n.h.
Since #include's are done in a way, where the preprocessor just takes the file that you are #include'ing and copy-pastes in the spot of #include statement, when n.h is being processed, it includes q.h, and then class Q appears before class N. Hence, it doesn't know what the class N is, since the compiler works in linear fashion (from the start of the file to the end).
So, it is my suggestion, to remove #include "q.h" statement from n.h, since the class N doesn't use class Q.

'ifinite' was not declared in this scope

I'm trying to compile shogun toolbox and I'm getting this fault
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h: In static
member function 'static int shogun::CMath::is_finite(double)':
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h:1255:20: er
ror: 'ifinite' was not declared in this scope
return ifinite(f);
function itself looks like this.
inline static int is_finite(double)
{
#if defined(isfinite) && !defined(SUNOS)
return ifinite(f);
#else
return finite(f);
#endif
}
I believe similar is described here: http://www.alecjacobson.com/weblog/?p=1768, but I'm not sure as I don't include cmath. Any idea what it can be?
Function is isfinite, not ifinite.
You don't include <cmath> but according to Shogun source here, it does include both <cmath> and <math.h> in the wrong order:
#include <shogun/base/SGObject.h>
#include <shogun/lib/common.h>
#include <cmath> <<<<<<
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/io/SGIO.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h> <<<<<<
So you are supposed to use std::isfinite.
I just downloaded shogun-3.0.0 from here, and there is no occurrence of the string “ifinite” anywhere in the source. The definition of is_finite in Math.h is:
/// checks whether a float is finite
inline static int is_finite(double f)
{
#if defined(isfinite) && !defined(SUNOS)
return isfinite(f);
#else
return finite(f);
#endif
}
If the errors and source text you entered into the question are correct, perhaps the sources you have were corrupted. You should download the source and try again.

stringstream was not declared in this scope

I'm having problem with stringstream.my visual studio nor linux g++ can understand stingstream. I've added sstream but it does'nt solve anything. I've worked with it before and really don't know what's up with it now?
#include <sstream>
#include <stdlib.h>
#include "SymbolTable.cpp"
#include "setjmp.h"
using namespace std;
jmp_buf *bfj;
int TOP , SP=3 ;
struct types{int int_val;float float_val;char char_val;bool bool_val;};
types DS[6400];
int main(){
...//some code here
label38 : stringstream s;
label39 : bfj = (jmp_buf *)"label65";
label40 : longjmp(*bfj,1);;
label41 : goto label43;
label42 : TOP=SP;
//some code here
}
I'm writing a compiler so the code is the output,that's why it may seams a bit odd.
If you include #include <sstream> then you must also reference the class by:
std::stringstream or declare using namespace std; before using it.
If you post more information we could provide more detailed help.
This code compiles fine for me under G++:
#include <sstream>
#include <stdlib.h>
#include "setjmp.h"
using namespace std;
jmp_buf *bfj;
int TOP , SP=3 ;
struct types{int int_val;float float_val;char char_val;bool bool_val;};
types DS[6400];
int main(){
label38 : stringstream s;
label39 : bfj = (jmp_buf *)"label65";
label40 : longjmp(*bfj,1);;
label41 : goto label43;
label42 : TOP=SP;
label43 : (void)0;
//some code here
}
The only difference is that I removed #include "SymbolTable.cpp", and added a label43.
So apparently, if it doesn't work for you, the problem is in some of the code you omitted. The //some code here parts or in SymbolTable.cpp
Of course, it also seems very suspicious that you're including a cpp file. That is most likely an error.

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!