I am trying to write a makeshift renderer for the tutorials on
this site. I have two classes SceneObject and RenderComponent. A SceneObject should contain a RenderComponent which should then draw the SceneObject. This is the code:
SceneObject.h
#ifndef _SCENE_OBJECT_H
#define _SCENE_OBJECT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <vector>
#include <iostream>
#include "..\headers\shader.h"
#include "..\headers\rendercomponent.h"
class SceneObject {
private:
glm::vec3 position;
float *vertices;
RenderComponent* renderComponent;
std::vector<Shader> shaders;
public:
SceneObject(glm::vec3, GLfloat*);
~SceneObject();
bool init();
RenderComponent& getRenderComponent() const;
};
#endif
SceneObject.cpp
#include "..\headers\sceneobject.h"
SceneObject::SceneObject(glm::vec3 pos, GLfloat* objectVertices) {
this->position = pos;
this->vertices = objectVertices;
renderComponent = new RenderComponent(*this);
}
SceneObject::~SceneObject() {}
RenderComponent& SceneObject::getRenderComponent() const {
return *renderComponent;
}
bool SceneObject::init() {
if (!renderComponent->initialize()) {
return false;
}
}
RenderComponent.h
#ifndef _RENDER_COMPONENT_H
#define _RENDER_COMPONENT_H
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <iostream>
#include "../headers/sceneobject.h"
class RenderComponent {
SceneObject &sceneObject;
public:
RenderComponent(SceneObject&);
RenderComponent(const RenderComponent&);
bool initialize();
void draw();
SceneObject& getSceneObject() const;
};
#endif
RenderComponent.cpp
#include "..\headers\rendercomponent.h"
RenderComponent::RenderComponent(SceneObject& obj)
:sceneObject(obj){}
RenderComponent::RenderComponent(const RenderComponent& ref)
: sceneObject(ref.getSceneObject()){}
bool RenderComponent::initialize() {}
void RenderComponent::draw() {}
SceneObject& RenderComponent::getSceneObject() const {
return sceneObject;
}
The following errors are produced.
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';' syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2664 'RenderComponent::RenderComponent(const RenderComponent &)': cannot convert argument 1 from 'SceneObject' to 'const RenderComponent &'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2061 syntax error: identifier 'SceneObject'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '*'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '&'
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
From what i understand, the compiler is saying that SceneObject is not a type. I think this where the problem is. Any help is appreciated.
You have sceneobject.h including rendercomponent.h and rendercomponent.h including sceneobject.h. With the include guards one of them doesn't know about the classes defined in the other header.
Remove the include from one or both headers and just forward declare the class(es) instead.
Related
Hey Im trying to implement state machine for my game and I wanted to test some things out.
So right now my machine has 2 states Pause and Menu each one of them prints one texture and if I press mouse it should change to other state. That should be simple and I think I implemented it right, but I am having 40 bugs and I think its connetcted to include's. Here is how my code looks:
Game.h
#pragma once
#include <SFML/Graphics.hpp>
#include "GameState.h"
#include "Menu.h"
#include "Pause.h"
class Game
{
public:
sf::RenderWindow* window;
sf::Event evnt;
GameState *state; // current state
sf::Texture text1;
sf::Texture text2;
sf::Sprite sText1;
sf::Sprite sText2;
void start(); // Main loop
void print1(); // Prints one graphic
void print2(); // Prints second one
void update();// Prints based on current state
void handleInput(); // check for input
Game();
};
Game.cpp
void Game::start()
{
while (window->isOpen())
{
handleInput();
update();
window->clear();
window->display();
}
}
Game::Game()
{
window= new sf::RenderWindow(sf::VideoMode(1900, 1080), "SFML works!");
text1.loadFromFile("Test.png");
sText1.setTexture(text1);
text2.loadFromFile("background.png");
sText2.setTexture(text1);
state = new Menu();
}
void Game::print1()
{
window->draw(sText1);
}
void Game::update()
{
state->Update(*this);
}
void Game::print2()
{
window->draw(sText2);
}
void Game::handleInput()
{
while (window->pollEvent(evnt))
{
GameState* tmp = state->handleInput(evnt);
if (tmp != NULL)
{
delete state;
state = tmp;
}
}
}
GameState.h
#pragma once
#include "Game.h"
class GameState
{
protected:
public:
virtual void Update(Game &gra )=0;
virtual GameState* handleInput(sf::Event evnt)=0;
};
Pause.h
#pragma once
#include "GameState.h"
#include "Menu.h"
class Pause:public GameState
{
public:
void Update(Game& gra);
GameState* handleInput(sf::Event evnt);
};
Pause.cpp
#include "Pause.h"
void Pause::Update(Game& gra)
{
gra.print1();
}
GameState* Pause::handleInput(sf::Event evnt)
{
if (evnt.type == sf::Event::MouseButtonPressed)
{
return new Menu();
}
else
{
return NULL;
}
}
Menu class works the same, just returns Pause class
Here are the errors, doubt that they will help
Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\GameState.h 11
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 9
Error C2660 'GameState::Update': function does not take 1 arguments sfml C:\Programowanie\Nauka\sfml\sfml\Game.cpp 52
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 9
Error C2504 'GameState': base class undefined sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 6
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 9
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C2504 'GameState': base class undefined sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 6
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 9
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2504 'GameState': base class undefined sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 6
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 9
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 10
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2440 'return': cannot convert from 'Pause *' to 'GameState *' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.cpp 15
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2504 'GameState': base class undefined sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 6
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 9
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 10
Error C2143 syntax error: missing ';' before '*' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2238 unexpected token(s) preceding ';' sfml C:\Programowanie\Nauka\sfml\sfml\Game.h 11
Error C2440 'return': cannot convert from 'Menu *' to 'GameState *' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.cpp 14
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\GameState.h 11
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Pause.h 9
Error C2061 syntax error: identifier 'Game' sfml C:\Programowanie\Nauka\sfml\sfml\Menu.h 9
You have a circular dependency where Game.h includes Gamestate.h and vice versa. In the former you can replace your full include
#pragma once
#include <SFML/Graphics.hpp>
#include "GameState.h" // full include
#include "Menu.h"
#include "Pause.h"
class Game
{
...
with a forward declaration instead
#pragma once
#include <SFML/Graphics.hpp>
#include "Menu.h"
#include "Pause.h"
class GameState; // forward declaration
class Game
{
...
Then you can #include "GameState.h" within your Game.cpp file instead
#include <iostream>
using namespace std;
int main()
{
cout<<"hello world!"<<endl;
return 0;
}
I am trying to run this simplest code in vs2010, but I received more than 100 errors when I was compiling it.
f:\vs2010\vc\include\fstream(14): error C2143: syntax error : missing ';' before '*'
f:\vs2010\vc\include\fstream(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
f:\vs2010\vc\include\fstream(16): error C2653: 'ios_base' : is not a class or namespace name
f:\vs2010\vc\include\fstream(16): error C2061: syntax error : identifier 'openmode'
f:\vs2010\vc\include\fstream(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Errors like these. What should I do?
try to include this lab in your code
#include "stdafx.h"
My code:
BlockyWorld.hpp
#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H
#include <CImg.h>
namespace logic {
class BlockyWorld {
public:
BlockyWorld( const CImg<float>* heightmap );
};
}
#endif // BLOCKYWORLD_H
BlockyWorld.cpp
#include "BlockyWorld.hpp"
namespace logic {
BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}
main.cpp
#include <CImg.h>
#include "logic/BlockyWorld.hpp"
//...
CImg<float> heigthMap;
logic::BlockyWorld world( &heigthMap );
//...
I get alot of errors while compiling:
main.cpp:
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
main.cpp(85): error C2664: 'logic::BlockyWorld::BlockyWorld(const logic::BlockyWorld &)' : cannot convert argument 1 from 'cimg_library::CImg<float>' to 'const int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
BlockyWorld.hpp & cpp
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
include\logic\blockyworld.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.cpp(4): error C2143: syntax error : missing ',' before '<'
I don't think it's a circular inclusion error which sometimes causes these kinds of errors for me=).
I must be defining constructor wrong or maybe I'm defining implementation wrong? Was searching for an answer for abount an hour now so I would really use an explanation now.
And just to clarify - I'm not a beginner c/c++ programmer but these templates are confusing :(
Have a nice day and thank your for your answers.
CImg appears to be part of the cimg_library namespace.
Either add using namespace cimg_library to the top of your BlockyWorld.hpp file, or change the function signature to use the namespace like so:
BlockyWorld( const cimg_library::CImg<float>* heightmap );
Along with πάντα ῥεῖ's suggestion of matching up your pointer and reference types.
I have create this squareList class I when I compile it it give me to many errors I don't know can someone help me to resolve those error
update: I comment all my code after ~square_list(){} and every error pinpoint to list data;
///#include "LinkedList.hpp"
#include <vector>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <list>
#include <iterator>
template <typename T_>
class square_list
{
typedef T_ value_type;
typedef std::size_t size_type;
typedef T_ & reference;
typedef T_ const & const_reference;
typedef T_ * pointer;
typedef T_ const * const_pointer;
typedef T_ * iterator;
typedef T_ const * const_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
//for header vector<pair<itr,unsindINT) header
list<T_> data;
square_list() {}
~square_list(){}
// bool empty(){
// if(this->begin() == nullptr && this->end() == nullptr)
// return 1;
// else
// return 0;
//}
//list<value_type>::iterator begin() {
// return data.begin();
//}
//list<value_type>::iterator end() {
// return data.end();
//}
};
Error 1 error C2143: syntax error : missing ';' before '<'
Error 4 error C2143: syntax error : missing ';' before '<'
Error 7 error C2143: syntax error : missing ';' before '<'
Error 10 error C2143: syntax error : missing ';' before '<'
Error 13 error C2143: syntax error : missing ';' before '<'
Error 16 error C2143: syntax error : missing ';' before '<'
Error 19 error C2143: syntax error : missing ';' before '<'
Error 22 error C2143: syntax error : missing ';' before '<'
Error 25 error C2143: syntax error : missing ';' before '<'
Error 28 error C2143: syntax error : missing ';' before '<'
Error 31 error C2143: syntax error : missing ';' before '<'
Error 34 error C2143: syntax error : missing ';' before '<'
Error 37 error C2143: syntax error : missing ';' before '<'
Error 40 error C2143: syntax error : missing ';' before '<'
Error 43 error C2143: syntax error : missing ';' before '<'
Error 46 error C2143: syntax error : missing ';' before '<'
Error 3 error C2238: unexpected token(s) preceding ';' Error 6 error
C2238: unexpected token(s) preceding ';' Error 9 error C2238:
unexpected token(s) preceding ';' Error 12 error C2238: unexpected
token(s) preceding ';' Error 15 error C2238: unexpected token(s)
preceding ';' Error 18 error C2238: unexpected token(s) preceding
';' Error 21 error C2238: unexpected token(s) preceding ';'
Error 24 error C2238: unexpected token(s) preceding ';'
Error 27 error C2238: unexpected token(s) preceding ';'
Error 30 error C2238: unexpected token(s) preceding ';'
Error 33 error C2238: unexpected token(s) preceding ';'
Error 36 error C2238: unexpected token(s) preceding ';'
Error 39 error C2238: unexpected token(s) preceding ';'
Error 42 error C2238: unexpected token(s) preceding ';'
Error 45 error C2238: unexpected token(s) preceding ';'
Error 48 error C2238: unexpected token(s) preceding ';' Error 2 error
C4430: missing type specifier - int assumed. Note: C++ does not
support default-int Error 5 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 8 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 11 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 14 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 17 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 20 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 23 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 26 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 29 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 32 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 35 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 38 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 41 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int Error 44 error C4430:
missing type specifier - int assumed. Note: C++ does not support
default-int Error 47 error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
it seems the compiler error is caused by the private construtor, this piece of code can compiler, hope it will help,
#include <vector>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <list>
#include <iterator>
using namespace std;
#define nullptr NULL
template <typename T_>
class square_list
{
public:
typedef T_ value_type;
typedef std::size_t size_type;
typedef T_ & reference;
typedef T_ const & const_reference;
typedef T_ * pointer;
typedef T_ const * const_pointer;
typedef T_ * iterator;
typedef T_ const * const_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
//for header vector<pair<itr,unsindINT) header
list<value_type> data;
square_list() {}
~square_list(){}
bool empty(){
if(this->begin() == nullptr && this->end() == nullptr)
return 1;
else
return 0;
}
/*list<T_>::iterator begin() {
return data.begin();
}
list<T_>::iterator end() {
return data.end();
}*/
};
int main()
{
square_list<int> sq_list;
return 0;
}
Your template parameter is T_, but you are referring to something called T which is undefined. Try changing
list<T> data<T>;
to
list<T_> data;
Also, try commenting out everything inside the class declaration and compiling. Then uncomment one line at a time and deal with any errors that appear after adding each line.
The first problem is this line
list<T> data<T>;
it should be
list<T> data;
Those template arguments are unnecessary and syntactically invalid. Also, you haven't defined the template argument T. Did you mean T_ (or value_type rather)?
Also, you need typename in return types of your begin() and end() methods as value_type is a dependent type:
typename list<value_type>::iterator begin();
typename list<value_type>::iterator end();
You can even take advantage of return type deduction:
auto begin() -> decltype(data.begin());
auto end() -> decltype(data.end());
list<value_type> data;
The code's problem is lack of std:: as follows:
std::list<value_type> data;
You also write using namespace std; before the class definition.
I keep receiving a long string of errors when I try to declare a vector in the header. I've looked around for awhile, but can't find a solution.
Here are the errors:
1>Compiling... 1>game.cpp 1>c:\users\legacyblade\documents\visual
studio 2008\projects\fourswords\fourswords\input.h(38) : error C2143:
syntax error : missing ';' before '<'
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2071:
'input::vector' : illegal storage class
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing
type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2238:
unexpected token(s) preceding ';' 1>main.cpp
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2143: syntax
error : missing ';' before '<' 1>c:\users\legacyblade\documents\visual
studio 2008\projects\fourswords\fourswords\input.h(38) : error C2071:
'input::vector' : illegal storage class
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing
type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2238:
unexpected token(s) preceding ';' 1>input.cpp
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2143: syntax
error : missing ';' before '<' 1>c:\users\legacyblade\documents\visual
studio 2008\projects\fourswords\fourswords\input.h(38) : error C2071:
'input::vector' : illegal storage class
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C4430: missing
type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\legacyblade\documents\visual studio
2008\projects\fourswords\fourswords\input.h(38) : error C2238:
unexpected token(s) preceding ';'
Here is the source code:
#include <vector>
#include <SFML/Graphics.hpp>
#ifndef _input_h
#define _input_h
class input
{
public:
input();
void update();
//----input keys----//
// Directions
bool upPress;
bool downPress;
bool leftPress;
bool rightPress;
// Actions
bool aPress;
bool bPress;
bool jumpPress;
bool shieldPress;
// Menu
bool startPress;
bool screenshotPress;
bool fullscreenPress;
//------------------//
private:
extern vector<sf::Keyboard::Key> keyBindings;
};
#endif
It gives me the same error with and without extern, and even if I change the type of thing inside the vector (even int).
Thank you so much for reading. It would be great if anyone could help. I need vectors to do what I'm wanting to do. Don't know why it's giving me such trouble. Any other type of variable in the same spot DOES NOT cause the error. Only vectors.
Just to add to what's been said, you need the namespace in the declaration because we usually don't want to bloat up header files with "using namespace std". So if you've seen vectors used elsewhere without std:: in front of it, the namespace was probably declared elsewhere.
You need to use the namespace for vector. Prefix vector with std::.
Also, extern on a class member semantically doesn't make any sense. Remove it.
std::vector<sf::Keyboard::Key> keyBindings;
extern vector<sf::Keyboard::Key> keyBindings;
should be
std::vector<sf::Keyboard::Key> keyBindings;