This question already has answers here:
C++: Syntax error C2061: Unexpected identifier
(5 answers)
Closed 9 years ago.
I'm getting this error:
1>b:\projects\c++\wolvesisland\wolvesisland\wolvesisland\board.h(22): error C2061: syntax error : identifier 'vector'
while trying to pass vector as an argument.
(it's about function move())
Code:
Board.h
#pragma once
#include <vector>
enum field_state {is_wolfm, is_wolff, is_rabbit, is_bush, is_nobody};
struct state{
field_state field;
int number;
};
class Board
{
private:
state island[20][20];
public:
Board(void);
~Board(void);
void fill(int,int,int,int, vector<LivingForm*>*);
state get_island(int,int);
void set_state(int,int,field_state,int);
};
and the function declaration in Board.cpp:
void Board::fill(int rabbit,int wolfm,int wolff,int bush,vector <LivingForm*> *creatures)
vector lives in the std namespace, so you need to refer to ut as std::vector:
void fill(int,int,int,int, std::vector<LivingForm*>*);
// ^^^
Related
This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 6 years ago.
I'm trying to compile some old C++ code which doesn't seem to be valid C++ anymore (VS2008 to VS2015). I've managed to narrow the problem down to something which looks like this.
class Any { };
class Parent
{
template < typename anyT>
class Child { };
};
template< typename parentT >
class Fail
{
typedef typename parentT::Child<Any> ChildT; // 2 errors
typename ChildT _child; // 2 errors
};
I get this compiler error from Visual Studio 2015.
Error C2059 syntax error: '<' TemplateTest d:\programming\templatetest\example.h 12
Error C2238 unexpected token(s) preceding ';' TemplateTest d:\programming\templatetest\example.h 12
Error C3646 '_child': unknown override specifier TemplateTest d:\programming\templatetest\example.h 13
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int TemplateTest d:\programming\templatetest\example.h 13
Where and why do I have to put the “template” and “typename” keywords?
typedef typename parentT::template Child<Any> ChildT;
ChildT _child;
I'm trying to separate my c++ code to one header and one cpp file, but the interpreter showing some errors.
Here is my code:
Password.h:
#ifndef PASSWORD_H
#define PASSWORD_H
class Password {
private:
string aliasName_;
int hashOfPassword_;
public:
void setAliasName(string aliasName);
void setHashOfPassword(int hashOfPassword);
string getAliasName() { return aliasName_; }
int getHashOfPassword() { return hashOfPassword_; }
};
#endif
Password.cpp:
#include <string>
#include "Password.h"
using std::string;
void Password::setAliasName(string aliasName) {
aliasName_ = aliasName;
}
void Password::setHashOfPassword(int hashOfPassword) {
hashOfPassword_ = hashOfPassword;
}
Errors:
Error C2065 'aliasName_': undeclared identifier X\password.cpp 7
Error C2511 'void Password::setAliasName(std::string)': overloaded member function not found in 'Password' X\password.cpp 6
Error C3646 'aliasName_': unknown override specifier X\password.h 6
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int X\password.h 6
Error C2061 syntax error: identifier 'string' X\password.h 9
Error C3646 'getAliasName': unknown override specifier X\password.h 11
Error C2059 syntax error: '(' X\password.h 11
Error C2334 unexpected token(s) preceding '{'; skipping apparent function body X\password.h 11
Anyone have any ideas ?
You need to move using std::string before the declaration of your class:
#ifndef PASSWORD_H
#define PASSWORD_H
#include <string>
using std::string;
class Password {
…
and remove it from your .cpp file.
Also, you might want to use #pragma once instead of the traditional #ifndef/#define/#endif and finally you might want to make your argument and methods const when need be.
You need to move
#include <string>
using std::string;
To inside Password.h, you can then remove these two lines from Password.cpp.
Just add std:: to every string in your header file and remember, you should never use using or using namespace there! You should also include all the headers to the file in which you use them.
Using: VS2010 : New Project -> Windows32 Console App -> Empty Project
Language: C++
Problem: I'm attempting a simple interface test with two classes that implement the interface through the 1 virtual method. I also attempt to use constructors within those two classes to set default values. The main problem occurs when attempting to create an object of either class; the IDE complains the identifier "class" is undefined.
I have checked tens of other posts with the same error but most of them are errors in instantiation(proper term?) where they use
Class obj();
instead of
Class obj;
another issue in the other threads was that the header files were not included in main.cpp AND the concrete class(proper term?). Still other issues involved the ordering of includes(base class should be first), or even improper use of constructors/deconstructors.
I have checked over those issues to see if they would work for me but have been unable to figure out what the problem is.
Any insight would be appreciated!
Error: (all in main.cpp)
Error 1 error C2065: 'Human' : undeclared identifier 6 1 interfaceGameTest
Error 2 error C2146: syntax error : missing ';' before identifier 'hum1' 6 1 interfaceGameTest
Error 3 error C2065: 'hum1' : undeclared identifier 6 1 interfaceGameTest
Error 4 error C2065: 'Orc' : undeclared identifier 7 1 interfaceGameTest
Error 5 error C2146: syntax error : missing ';' before identifier 'orc1' 7 1 interfaceGameTest
Error 6 error C2065: 'orc1' : undeclared identifier 7 1 interfaceGameTest
Error 7 error C2065: 'hum1' : undeclared identifier 10 1 interfaceGameTest
Error 8 error C2227: left of '->getHP' must point to class/struct/union/generic type 10 1 interfaceGameTest
Code:
IPc.h
#ifndef IPC_H_HAS_BEEN_INCLUDED
#define IPC_H_HAS_BEEN_INCLUDED
class IPc {
private:
int hp;
int mana;
int endurance;
public:
void setHP(int h) { hp = h; }
void setMP(int m) { mana = m; }
void setEnd(int e) { endurance = e; }
int getHP() { return hp; }
int getMP() { return mana; }
int getEnd() { return endurance; }
virtual int Attack() = 0;
};
#endif
IPc.cpp
#include "IPc.h"
class Human: public IPc
{
public:
Human::Human()
{
this->setHP(10);
this->setMP(5);
this->setEnd(10);
}
Human::~Human(){}
int IPc::Attack() // I have tried just "int Attack()" as well
{
return 1;
}
};
class Orc: public IPc
{
public:
Orc::Orc()
{
this->setHP(20);
this->setMP(0);
this->setEnd(20);
}
Orc::~Orc() {}
int IPc::Attack()
{
return 5;
}
};
main.cpp
#include <iostream>
#include "IPc.h"
int main()
{
Human hum1; // error Human undefined
Orc orc1; // error Orc undefined
int humHP = 0;
humHP = hum1->getHP();
std::cout << "Human HP is: " << humHP << std::endl;
return 0;
}
Edit
I placed
class Human: public IPc { virtual int Attack() };
inside IPc.h (after the base class) and before #endif(also did one for Orc). I also changed
humHP = hum1->getHP();
to
humHP = hum1.getHP();
Which seems to have cleared up the previous issues(Thanks Mat and Rahul). I am now getting a 'class' type redefinition error. I believe this has to do with the include guards. Do I need to surround each individual class with its own set of guards in the .h file or have I implemented them improperly perhaps?
Edit2
I placed both class declarations in their own header files with their own guards. This seems to have resolved the issue of " 'class' type redefinition". Though there is now an error of unresolved external symbol on the virtual attack calls for both derived classes.
You are not allowed to use qualified names in class member declarations. I.e. when declaring the constructor of class Human inside class Human, you are not allowed to call it Human::Human(). It should be just Human(). The same applies to all other methods.
Trying to declare method int IPc::Attack() inside other classes makes no sense at all. It should be just int Attack().
You declared classes Human and Orc inside IPc.cpp. These classes are only visible inside IPc.cpp and nowhere else. In C++ language class declarations for program-wide classes are typically placed in header files, just how you did it with IPc class.
This question already has answers here:
Why am I getting "error: expected '}'" in C++ but not in C?
(3 answers)
Closed 9 years ago.
I have the following code in a header file:
enum {false,true};
and I have my main function in main.c. if I change the extention to main.cpp
I get the following error:
Error C2059: syntax error 'constant'
Im using visual c++, any Idea why`?
true and false are keywords representing constant values in C++. You cannot use them to name things such as enum values.
As an example, the following would compile
enum { false_, true_ };
int main() {}
false and true are reserve words in C++. You can't redefine it as variable.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to create a set function that will take in a shared pointer and set it equal to another shared pointer.
This is the shared pointer and the set function that i have declared on my header file
class Shape
{
public:
Shape();
Gdiplus::Point start;
Gdiplus::Point end;
std::shared_ptr<Gdiplus::Pen> m_pen;
virtual LRESULT Draw(Gdiplus::Graphics * m_GraphicsImage) = 0;
void setPen(std::shared_ptr<Gdiplus::Pen> pen2);
void setStart(int xPos, int yPos);
void setEnd(int xCor, int yCor);
};
but when i try to implement it in my cpp i get an error saying that my "declaration is incompatible with void setPen declared on .h". it also says m_pen is unedintified on my cpp file.
#include<memory>
#include "stdafx.h"
#include "resource.h"
#include "ShapeMaker.h"
void Shape::setPen(std::shared_ptr<Gdiplus::Pen> pen2)
{
m_pen = pen2;
}
void Shape::setStart(int xPos, int yPos)
{
start.X = xPos;
start.Y = yPos;
}
void Shape::setEnd(int xCor, int yCor)
{
end.X= xCor;
end.Y = yCor;
}
That's literally all i have. the stdax.h includes
#include <atlwin.h>
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <atlctrlw.h>
#include <atlscrl.h>
#include <GdiPlus.h>
Errors that I get:
shapemaker.h(11): error C2039: 'shared_ptr' : is not a member of 'std'
shapemaker.h(11): error C2143: syntax error : missing ';' before '<'
shapemaker.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
shapemaker.h(11): error C2238: unexpected token(s) preceding ';'
.h(16): error C2039: 'shared_ptr' : is not a member of 'std'
shapemaker.h(16): error C2061: syntax error : identifier 'shared_ptr'
shapemaker.cpp(9): error C2511: 'void Shape::setPen(std::tr1::shared_ptr<_Ty>)' : overloaded member function not found in 'Shape'
I'm posting my answer from the comments for any visitors.
The problem is at the beginning of the cpp file.
#include<memory>
#include "stdafx.h"
#include "resource.h"
#include "ShapeMaker.h"
MSVC++ demands that the precompiled header "stdafx.h" precede any other code in your source files.
The #include<memory> must be removed, and instead placed in "ShapeMaker.h" where it is first needed.