I am trying my hands on sample codes from a book, and so I am not entirely sure what I may have wrong in the header file I have so far.
I keep getting the following error messages.
Error 2 error C2061: syntax error : identifier 'streambuf'
Error 1 error C2504: 'ios' : base class undefined
Error 5 IntelliSense: identifier "streambuf" is undefined
// StdAfx.h HEADER FILE
**************************
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <strstream>
#include <iomanip>
#include <ios>
#include <stdio.h>
#include <tchar.h>
#include "targetver.h"
// Conios HEADER FILE
**************************
#include "Stdafx.h"
class Conios :virtual public ios{
protected:
public:
Conios(void);
~Conios(void);
Conios(streambuf* Buffer);
};
ios is in the std-namespace. So either use use namespace std; or extend from std::ios instead of just ios.
If you are using use namespace use it only in your implementation-files like *.cpp or *.cxx, do not write use namespace ... your header files - ever!.
And the same goes for streambuf.
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
while working on a personal project on a new language, I 've come across a annoying compiling time error where a static member of my constants.h (constants class) doesn't find the class name (Quark which is a derived class of Particle, those two in the same file "Particle.h") used in parameter...
Here is the compilation error output:
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(46): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(51): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
Here the crashing code:
constants.h
#pragma once
#include "stdafx.h"
#include <vector>
#include "Particle.h"
class constants
{
public:
static enum nucl
{
proton = 0
,neutron = 1
};
static std::vector<Quark> getNuclVal(nucl type)
{
if (type == nucl::proton)
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("up")), Quark(std::string("down")) });
return result;
}
else
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("down")), Quark(std::string("down")) });
return result;
}
};
// More functions and constants
};
Particle.h
#pragma once
#include <vector>
#include "Entity.h"
#include "constants.h"
class Particle : public Entity
{
private:
// some stuff
public:
// some stuff
};
class Quark : public Particle
{
public:
Quark(std::string &const);
};
and there is my Quark class definition in Particle.cpp
#include "stdafx.h"
#include "Particle.h"
#include "constants.h"
#include <string>
#include "Entity.h"
Particle::Particle(std::string &const name, Size size)
: Entity::Entity()
, m_charge(0) //en e
, m_mass(0) //en electron Volt (eV)/c²
, m_Size(size)
, m_name(name)
{
};
Quark::Quark(std::string &const name) // declaration stuff
: Particle(name, Size::Sub_Atomic)
{
constants::quark type = constants::getQuarkByName(m_name);
setMass(constants::getMass(type));
setCharge(constants::getCharge(type));
}
// various stuffs
There is my precompiled header which include every sources I need in my program (to let you see compilation order) stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
// TODO: reference additional headers your program requires here
#include "Math.h"
#include "SciNumber.h"
#include "PhyEngine.h"
#include "Options.h"
#include "ConfigFile.h"
#include "Chameleon.h"
#include "Entity.h"
#include "Particle.h"
#include "constants.h"
I wonder if I need to separate my derived classes of Particle to specific files or not (It would add a lot of files..... T-T), what do you think about it?
here is my class diagram (open with Visual studio) to help you visualize the whole project: download it here via my dropbox
You have a circular dependency in your headers. Particle.h includes constants.h and constants.h includes Particle.h. You'll need break the loop.
You get the error because when Particle.h is included, it'll at first include constants.h. Then constants.h will try to include Particle.h. #pragma once will prevent infinite recursion, but also means that rest of the Particle.h is not yet included before the contents of the constants.h. Thus the compiler complains about missing declarations.
You should be able to break the loop by breaking your headers and possibly classes into smaller ones*. The static member functions of constants are not strictly related to each other, are they? In fact, from what I've seen, it should probably be a namespace rather than a class**. Stuff in namespace can be conviniently declared in separate headers while a class must be defined entirely in one.
*Assuming you actually need to. If Particle.h doesn't depend on constants.h, then simply remove the include.
**I may be wrong about that since you didn't show the entire class.
Break off the part of constants that depends on Quark (and anything that depends on anything that depends on Quark, recursively) into another class in another header and don't include that one inside Particle.h. Or break off parts of Particle.h that depend on constants.h and don't include that one in constants.h.
stdafx.h also includes constants.h which in turn includes stdafx.h. You'll need to fix all circular includes.
Try moving
#include "constants.h"
to the bottom of Particle.h. What currently happens is that Particle.h goes through constants.h before it has declared the types.
constants.h is not re-including particle.h because of pragma #once (without which you would have circular dependencies and nothing would work anyway).
You also don't need the #include "constants.h" in stdafx.h as it is already included in Particle.h
I hope this helps!
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.
I need to write small tool in C++
I've never used C++ as programming language before (I have couple years of Java dev experience) and .NET
I've started a new project in VS , when I am adding in my Header file of my class
#include <windows.h> I am getting the following error:
Error 1 error C2143: syntax error : missing ';' before '*' c:\program files\microsoft sdks\windows\v7.0a\include\servprov.h 96 1 CppLog
For now my class even doesn't have any real functions and looks like
in header
class TheTool
{
public :
void Foo();
};
in cpp
void TheTool::Foo(){};
and the project doesn't get compiled.
plz any suggestions ? Maybe a compiler doen't set up good ?
this is how the Header file looks like
#pragma once
#include "stdafx.h"
#include <stdio.h>
//#include <Windows.h>
//#include <winuser.h>
//#include <windowsx.h>
//#include <time.h>
class TheTool
{
public :
void Foo();
};
When I am uncommenting the include I am starting to get this compilation error.
BTW , how can i know the compiller setting ?
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!