I'm having a hard time understanding why I get an error when I try to compile this.
Do I need to post my main too?
#ifndef SEARCHABLEADT_H
#define SEARCHABLEADT_H
#include <string>
template <typename T>
class SearchableADT
{
public:
virtual int loadFromFile(string filename) = 0;
//virtual void clear(void) = 0;
virtual void insertEntry(T value) = 0;
virtual void deleteEntry(T value) = 0;
virtual bool isThere(T value) = 0;
virtual int numEntries(void) = 0;
};
#endif
error is:
c:\users****\documents\visual studio 2012\projects\headersearchableadt\headersearchableadt\searchableadt.h(10): error C2061: syntax error : identifier 'string'
You could also use the next declaration:
#include <string>
using namespace std;
Although some one could argue that that's a bad coding habit.
Try: virtual int loadFromFile(std::string filename) = 0;.
Note the std::.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i fixed it there is no need for this question now thank you for your help
i have some errors on my program in vs 2017 that sometimes go and sometimes stay. i think it is not recognising my classes and i think its something to do with the #include not working properly as in the past i have had a single similar error before (something like ; missing before identifier) which stopped when I forward declared instead of #included please help.
form class
#pragma once
#include "IContain.h"
#include <vector>
#include "Rect.h"
class Form : public IContain
{
public:
Form();
~Form();
bool paint() override;
bool init() override;
Rect<int> Rect() override;
bool hasfocus() override;
Graphics& getgraphics() override;
bool add(IStaticControl & control) override;
bool remove(IStaticControl & control) override;
bool update() override;
pixelvec mousepos() override;
private:
std::vector<IStaticControl> controls;
};
control interface class
#pragma once
#include "Graphics.h"
#include "Rect.h"
#include "IContain.h"
class IStaticControl
{
public:
IStaticControl();
~IStaticControl();
//actions
virtual bool init() = 0;
virtual bool paint(Graphics& gfx) = 0;
virtual void update() = 0; // check for input that matches this' conditions. check container's input
virtual void show() = 0;
virtual void hide() = 0;
//info
virtual Rect<int> rect() = 0;
virtual IContain& Container() = 0;
virtual bool rezizeable() = 0;
virtual bool hasfocus() = 0;
};
event class
#pragma once
#include <vector>
#include "EventBase.h"
#include <queue>
template<typename EventArgsT>
class Myevent
{
public:
Myevent(size_t handlerCount, size_t instanceCount, std::queue<EventBase>&
eventqueue) : Eventintances(instanceCount), handlers(handlerCount) {this->eventqueue = eventqueue};
~Myevent() {};
inline void raise(EventArgsT args) {eventqueue.push(Inst(args, handlers));};
size_t addhandler(void(*handler)(EventArgsT) , bool& success = false) {
size_t size = handlers.size;
for (size_t i; i >= size; i++) {
if (handlers[i] = NULL) { handlers[i] = handler; success = true;
return i; }
}
return 0;
};
void removehandler(size_t i) { handlers[i] = NULL };
class Inst : EventBase {
EventArgsT args;
public:
Inst(EventArgsT args, std::vector<void(*)(EventArgsT)>& handlers) {
this->args = args;
this->handlers = handlers;
};
// go to each function in handlers and invoke them with args
void process() override {};
private:
std::vector<void(*)(EventArgsT)>& handlers;
};
private:
std::vector<void(*)(EventArgsT)> handlers;
std::vector<Inst> Eventintances;
std::queue<EventBase>& eventqueue;
};
control container interface class
#pragma once
#include "Myevent.h"
#include "graphics.h"
#include "IStaticControl.h"
class IContain
{
public:
virtual bool paint() = 0;
virtual bool init() = 0;
virtual Rect<int> rect() = 0;
virtual bool hasfocus() = 0;
virtual Graphics& getgraphics() = 0;
virtual bool add(IStaticControl & control) = 0;
virtual bool remove(IStaticControl & control) = 0;
virtual bool update() = 0;
virtual pixelvec mousepos() = 0;
// same for keyboard input
private:
};
These are the errors
Error C2433 'IStaticControl::IContain': 'virtual' not permitted on data declarations c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2433 'IStaticControl::IContain': 'virtual' not permitted on data declarations c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2061 syntax error: identifier 'IStaticControl' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\icontain.h 17
Error C2061 syntax error: identifier 'IStaticControl' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\icontain.h 18
Error C2143 syntax error: missing ';' before '&' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2143 syntax error: missing ';' before '&' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2238 unexpected token(s) preceding ';' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
Error C2238 unexpected token(s) preceding ';' c++ Game c:\users\home\documents\visual studio 2017\projects\c++ game\c++ game\istaticcontrol.h 21
please help
update: there are 10 errors
the code has changed very slightly
You have cyclic dependencies. IStaticControl.h includes IContain.h, and IContain.h includes IStaticControl.h.
(You neglected to mention the names of your files, but they can be deduced from the error messages.)
This is why IContain is undefined in this line:
virtual IContain& Container() = 0;
... so the compiler thinks you're trying to declare a member variable called IContain as an implicit int:
virtual int IContain; & Container() = 0;
This is why it's complaining about data members being virtual, implicit int, and syntax errors involving ; and &.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I recently got stuck in a situation. In the first version, everything is implemented in header file and it works fine. In the second version when i tried to separate implementation from header declarations, I got many errors. In the below lines, i m going to demonstrate problem. Thanks in advance..
First Version (it works fine!)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
public:
class CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
private:
CameraValue(const CameraProperties& properties)
:mProperties(properties)
{}
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Second Version (Don't work)
cameravalue.h
#ifndef CAMERAVALUE_H
#define CAMERAVALUE_H
#include <string>
class CameraValue
{
private:
class CameraProperties;
public:
class CameraBuilder;
private:
CameraValue(const CameraProperties& properties);
CameraProperties mProperties;
};
#endif // CAMERAVALUE_H
cameravalue.cpp
#include "cameravalue.h"
#include <string>
class CameraValue::CameraProperties
{
private:
CameraProperties()
: mId(-1),
mName(),
mAddress(),
mExposure(),
mFocus()
{}
int mId;
std::string mName;
std::string mAddress;
std::string mExposure;
long long mFocus;
friend class CameraValue;
friend class CameraBuilder;
};
class CameraValue::CameraBuilder
{
public:
CameraBuilder(int id)
{
mProperties.mId = id;
}
CameraBuilder& setName(std::string& name)
{
mProperties.mName = name;
return *this;
}
CameraBuilder& setAddress(std::string& adress)
{
mProperties.mAddress = adress;
return *this;
}
CameraBuilder& setExposure(std::string& exposure)
{
mProperties.mExposure = exposure;
return *this;
}
CameraBuilder& setFocus(int focus)
{
mProperties.mFocus = focus;
return *this;
}
CameraValue build()
{
return CameraValue(mProperties);
}
private:
CameraProperties mProperties;
};
CameraValue::CameraValue(const CameraProperties& properties)
: mProperties(properties)
{}
main.cpp
#include "cameravalue.h"
#include <iostream>
int main(int argc, char *argv[])
{
CameraValue cm = CameraValue::CameraBuilder(1).setName(std::string("Huseyin")).build();
return 0;
}
Compile Errors
cameravalue.cpp
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\cameravalue.cpp(74)
: error C2440: 'initializing' : cannot convert from 'const
CameraValue::CameraProperties' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..\BuilderPattern\cameravalue.cpp(74) : error C2439:
'CameraValue::mProperties' : member could not be initialized
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : see declaration of 'CameraValue::mProperties'
c:\users\huseyin\documents\builderpattern\cameravalue.h(20) : error
C2079: 'CameraValue::mProperties' uses undefined class
'CameraValue::CameraProperties' ..\BuilderPattern\main.cpp(9) : error
C2440: '<function-style-cast>' : cannot convert from 'int' to
'CameraValue::CameraBuilder'
Source or target has incomplete type ..\BuilderPattern\main.cpp(9) : error C2228: left of '.setName' must
have class/struct/union ..\BuilderPattern\main.cpp(9) : error C2228:
left of '.build' must have class/struct/union
..\BuilderPattern\main.cpp(9) : error C2512: 'CameraValue' : no
appropriate default constructor available
..\BuilderPattern\main.cpp(10) : error C2039: 'getName' : is not a
member of 'CameraValue'
c:\users\huseyin\documents\builderpattern\cameravalue.h(8) : see declaration of 'CameraValue'
Definition of class CameraBuilder should be visible in main.cpp, so you can't just forward-declare it in cameravalue.h. But you can make the definitions of its member functions out-of-line:
// cameravalue.h
class CameraValue {
class CameraBuilder {
public:
CameraBuilder(int id);
...
};
};
// cameravalue.cpp
CameraValue::CameraBuilder::CameraBuilder(int id) {
...
}
I have a class "GameOverState" which has a private member
static const std::string s_gameOverID;
In GameOverState.cpp I am initialising as :
const std::string GameOverState::s_gameOverID = "GAMEOVER";
I am getting the following errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2440: 'initializing' : cannot convert from 'const char [9]' to 'int'
error C2377: 'std::string' : redefinition; typedef cannot be overloaded with any other symbol
error C2373: 's_gameOverID' : redefinition; different type modifiers
error C2143: syntax error : missing ';' before 'GameOverState::s_gameOverID'
I have a PlayState class/PauseState class which have the same implementation which are working fine. How do I fix this bug??
GameOverState.h
#pragma once
#include "GameState.h"
#include "PlayState.h"
#include "MenuState.h"
#include "PauseState.h"
#include "AnimatedGraphic.h"
#include <string>
class GameObject;
class GameOverState : public GameState
{
public:
virtual void update();
virtual void render();
virtual bool onEnter();
virtual bool onExit();
virtual std::string getStateID() const { return s_gameOverID; }
private:
static void s_gameOverToMain();
static void s_restartPlay();
static const std::string s_gameOverID;
std::vector<GameObject*> m_gameObjects;
}
GameOverState.cpp
#include "GameOverState.h"
const std::string GameOverState::s_gameOverID = "GAMEOVER";
void GameOverState::s_gameOverToMain()
{
TheGame::Instance()->getStateMachine()->changeState(new MenuState());
}
void GameOverState::s_restartPlay()
{
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}
bool GameOverState::onEnter()
{
if (!TheTextureManager::Instance()->load("assets/gameover.png", "gameovertext", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/main.png", "mainbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/restart.png", "restartbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
GameObject* gameOverText = new AnimatedGraphic(new LoaderParams(200, 100, 190, 30, "gameovertext"), 2);
GameObject* button1 = new MenuButton(new LoaderParams(200, 200, 200, 80, "mainbutton"), s_gameOverToMain);
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, "restartbutton"), s_restartPlay);
m_gameObjects.push_back(gameOverText);
m_gameObjects.push_back(button1);
m_gameObjects.push_back(button2);
std::cout << "entering PauseState\n";
return true;
}
You're missing the semicolon after the definition of GameOverState.
The preprocessor runs before compilation and basically just copy pastes the content of the header in the source file, altough we can't see that. An error resulting from a broken header can thus be pretty misleading.
It's legal to have class definitions inside a variable definition and the position of specifiers (like static) is not limited to the beginning of a declaration, either (for example, int const static x = 0; is fine).
So, your code looks like this to the compiler:
class GameOverState {} static const std::string GameOverState::s_gameOverID = "GAMEOVER";
Hopefully the errors make more sense now.
As everyone else has said, you're probably missing the #include line in your header if your other two classes are working fine. It's presuming and expecting an int, so that seems the case.
Make sure #include<string> is in your header
I have seen this question: "Class type redefinition" error between header and source files
If you'll just say a duplicate of the above question, please check first the code below.
The problem is that the "Class type redefinition" should not happened because I think I have done it properly.
Here is the code.
TTSpeech.h
#include <wtypes.h>
#include <string>
#include <vector>
class TTSpeech
{
public:
enum State
{
State_Loaded,
State_Unloaded,
};
TTSpeech();
virtual ~TTSpeech();
virtual bool isAvailable() = 0;
virtual bool load() = 0;
virtual bool unload() = 0;
virtual bool isLoaded() const = 0;
virtual bool say(const std::string &sentence) = 0;
virtual bool setVolume(int volume) = 0;
virtual bool setPitch(int pitch) = 0;
virtual bool setRate(int rate) = 0;
virtual bool setVoice(const std::string &voice) = 0;
virtual std::vector<std::string> getVoices() const = 0;
};
TTSpeech.cpp
#include "TTSpeech.h"
TTSpeech::TTSpeech()
{
//code
}
TTSpeech::~TTSpeech()
{
//code
}
The only unusual thing that I have done is to remove the files from the solution, relocate the above file to a folder because of this issue: cannot open include no such file or directory. Then re-add the files to the solution. I'm using Visual Studio 2012 running of Windows 8
I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.