C++ Error C3646, C2059, and C2238 Visual Studio 2015 (Community) [duplicate] - c++

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
I have been trying for some time (in vain) to track down the cause of the following errors all thrown at the declaration of 'checkCollision()' in "PolygonGameObject.h" when I try to compile as a static library:
Error C3646 'checkCollision': unknown override specifier
Error C2059 syntax error: ')'
Error C2238 unexpected token(s) preceding ';'
In an attempt to track down the error, I have renamed the function, removed all function parameters, removed all content from the offending function (except a return statement returning an object created by the default constructor of the desired return type). For brevity, I have removed all content from the original program while still causing the same errors in an attempt to find the cause of the errors.
I am using Visual Studio 2015 Community running on Windows 7. I am compiling the source files as a static library. Below are the stripped down source files with which I have produced the error:
CollisionData.h
#ifndef COLLISIONDATA_H
#define COLLISIONDATA_H
#include "Vec2D.h"
#include "PolygonGameObject.h"
#include <list>
struct CollisionData
{
double penetrationDepth;
Vec2D collisionNormal;
std::list<std::size_t> involvedIndicesA;
std::list<std::size_t> involvedIndicesB;
CollisionData();
CollisionData(double penetrationDepth, Vec2D collisionNormal, std::list<std::size_t> involvedIndicesA, std::list<std::size_t> involvedIndicesB);
~CollisionData();
static CollisionData noCollision();
};
#endif
CollisionData.cpp
#include "CollisionData.h"
using namespace std;
CollisionData::CollisionData()
{
}
CollisionData::CollisionData(double penetrationDepth, Vec2D collisionNormal,
list<size_t> involvedIndicesA, list<size_t> involvedIndicesB):
penetrationDepth(penetrationDepth), collisionNormal(collisionNormal),
involvedIndicesA(involvedIndicesA), involvedIndicesB(involvedIndicesB)
{
}
CollisionData::~CollisionData()
{
}
CollisionData CollisionData::noCollision()
{
return CollisionData(-1, Vec2D(0,0), list<size_t>(), list<size_t>());
}
PolygonGameObject.h
#ifndef POLYGONGAMEOBJECT_H
#define POLYGONGAMEOBJECT_H
#include "CollisionData.h"
class PolygonGameObject
{
protected:
double restitution;
double density;
double invMass;
public:
PolygonGameObject();
virtual ~PolygonGameObject();
static CollisionData checkCollision();
};
#endif
PolygonGameObject.cpp
#include "PolygonGameObject.h"
#include "CollisionData.h"
#include <limits>
#include <cmath>
#include <algorithm>
PolygonGameObject::PolygonGameObject()
{
}
PolygonGameObject::~PolygonGameObject()
{
}
CollisionData PolygonGameObject::checkCollision()
{
return CollisionData();
}
Thanks for your time!

You have a circular include, where PolygonGameObject.h contains #include "CollisionData.h" and vice versa. That is not possible.
I can't see that any PolygonGameObject is used in CollisionData.h, so you could just remove the unneeded include from that header.

Related

Getting C2760 syntax error: unexpected token 'int', expected 'expression' for std::max<int>(...) [duplicate]

This question already has answers here:
Problem calling std::max
(2 answers)
Closed 4 years ago.
I am trying to build a DLL with C exports for the MathGeoLib library to be consumed from C#.
The code I've written so far:
#include "stdafx.h"
#include "MathGeoLib/MathGeoLib.h"
OBB* OptimalEnclosingOBB(const vec* pointArray, int numPoints)
{
const auto obb1 = OBB::OptimalEnclosingOBB(pointArray, numPoints);
const auto obb2 = new OBB(obb1);
return obb2;
}
The stdafx.h file:
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>
But when I compile, I get the following error even though I defined NOMINMAX:
1>c:\build\mathgeolib\include\mathgeolib\geometry\quadtree.inl(699): error C2760: syntax error: unexpected token 'int', expected 'expression'
This is the line where Visual Studio 2017 is choking on:
https://github.com/juj/MathGeoLib/blob/master/src/Geometry/QuadTree.inl#L699
template<typename T>
int QuadTree<T>::NumNodes() const
{
return std::max<int>(0, nodes.size() - 3); // The nodes rootNodeIndex+1, rootNodeIndex+2 and rootNodeIndex+3 are dummy unused, since the root node is not a quadrant.
}
Question:
How can I fix this error ?
The line #include <algorithm> is required for use of std::max .

Using a C++ Class in Visual Studio - undeclared identifier error [duplicate]

This question already has answers here:
Why stdfax.h should be the first include on MFC applications? [duplicate]
(3 answers)
Closed 5 years ago.
I expect this is incredibly simple but can't for the life of me figure out whats wrong. I'm new to C++ and I've created a C++ class in visual studio and am trying to use it in the main method of another file. I've stripped everything back to the bare minimum but still I can't get it to run. The first compile error I get is 'Test': undeclared identifier. If I remove 'Test test;' from App.cpp it all compiles fine.
Below is the code. Can anybody help?
App.cpp:
#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
Test test;
//cout << test.getNumber() << endl;
return 0;
}
Test.h:
#pragma once
class Test
{
private:
int number;
public:
int getNumber();
Test();
~Test();
};
Test.cpp:
#include "stdafx.h"
#include "Test.h"
int Test::getNumber()
{
return number;
}
Test::Test()
{
this->number = 1;
}
Test::~Test()
{
}
The problem is that "stdafx.h" is pre-compiled.
Visual c++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The solution is to move it to be the first include.
You can read more about it in the wiki page of Precompiled header.

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Generator

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.

ERROR C2039: 'vector': is not a member of 'std'

I am new to C++ and I am trying to make a little dungeon crawler game. Currently I have multiple vectors declared in my header files but they seem to give multiple errors. I have tried searching for this problem on StackOverflow but the answers don't really seem to work.
Here is one of my header files: (Hero.h)
#pragma once
class Hero {
public:
Hero();
std::string name;
int experience;
int neededExperience;
int health;
int strength;
int level;
int speed;
std::vector<Item> items = std::vector<Item>();
void levelUp();
private:
};
Here is my .cpp file: (Hero.cpp)
#include "stdafx.h"
#include <vector>
#include "Hero.h"
#include "Item.h"
Hero::Hero() {
}
void Hero::levelUp()
{
};
Like I said I am new to C++ so there might be a lot more wrong with my code than I know. This is just a test.
Below are the errors that are shown in the Error list of Visual Studio 2015:
Error C2039 'vector': is not a member of 'std' CPPAssessment hero.h 13
Error C2143 syntax error: missing ';' before '<' CPPAssessment hero.h 13
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int CPPAssessment hero.h 13
Error C2238 unexpected token(s) preceding ';' hero.h 13
Include <vector> in your Hero.h header and consider removing it from your Hero.cpp file as mentioned in the comments below.
std::vector<Item> items = std::vector<Item>(); declares a complete type.
Therefore the compiler needs to know the declaration of std::vector at that point (amongst other things, it's required to establish the compile-time evaluable constant sizeof Hero). The solution is to #include <vector> in the header hero.h, not the source file.
//.h
template<class _Ty>
class std::allocator;
template<class _Ty, class _Alloc = std::allocator<_Ty> >
class std::vector;
//this line in .cpp file. Put it before custom header files.
#include<vector>
Tested in vs 2017.
In this way, no header file included in your custom header file.
But not sure why the save way for stack does not work.
For further information, https://github.com/YagaoDirac/snippet-set-for-cpp. And a discord link inside.

C++ - Static function using vector<derivedclass> don't find declared identifier

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!