Vector inside a class. C2065 error - c++

I have vector of my own objects inside a class. But when i want to do something with this vector, i have error :/
game.h
#include "renderSystem.h" //there only #include "console_color.h"
#include "level.h" //there only #include "renderSystem.h"
#include "gameObject.h"
class Game {
vector<GameObject> objects;
//something
public:
Game();
//something
};
game.cpp
void Game::initialize() {
GameObject playerObject(GameObjectType_Player);
objects.insert(objects.end(), playerObject);
//something
}
gameObject.h
#include "renderSystem.h"
#include "level.h"
class GameObject {
//something
public:
GameObject(GameObjectType _type);
GameObject() : GameObject(GameObjectType_None) {};
//something
};
And errors are (i cant give you logs, because they are on russian :/ )
C2065 at game.cpp "objects.insert"
C2143 at game.h "vector<GameObject> objects"
C2228 at game.cpp "objects.insert"
C2238 at game.h "vector<GameObject> objects"
C4430 at game.h "vector<GameObject> objects"
I checked #includes, but may be i'm stupid :/
And in every file i have #pragma once
In renderSystem and level i wrote only includes
Wtf?!
Visual studio 2015
//And i have 0:43 AM, so may be i just need to sleep :/

You have two issues here, one, you're not including the vector header file. The second is that the vector class is in the std:: namespace in C++. Here are two possible fixes to this.
#include <vector> /* STL vector */
#include "renderSystem.h" //there only #include "console_color.h"
#include "level.h" //there only #include "renderSystem.h"
#include "gameObject.h"
class Game {
private:
std::vector<GameObject> objects;
public:
Game();
};
Or
#include <vector> /* STL vector */
#include "renderSystem.h" //there only #include "console_color.h"
#include "level.h" //there only #include "renderSystem.h"
#include "gameObject.h"
using namespace std;
class Game {
vector<GameObject> objects;
//something
public:
Game();
//something
};

Related

expected class-name before '{' token, then invalid use of incomplete type 'class class_name' when pre-declaring

I'm trying to add inheritance to my objects based on SFML and I can't solve a problem with errors
*expected class-name before '{' token*
and when I try to solve it by pre-declaring my parent class I'm getting stopped by
*invalid use of incomplete type 'class Object'*
I made Rectangle and Circle class with Object as parent class
Object.hpp
#ifndef OBJECT_HPP_INCLUDED
#define OBJECT_HPP_INCLUDED
#include "../MainClass.hpp"
#include "../Data/Data.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
class Object
{
public:
Object();
virtual ~Object();
virtual void update() = 0;
virtual void draw() = 0;
};
#endif // OBJECTPARENT_HPP_INCLUDED
in Object.cpp file I only have definitions of constructor and deconstructor
Rectangle.hpp
#ifndef RECTANGLE_HPP_INCLUDED
#define RECTANGLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Rectangle : public Object
{
public:
Rectangle(/*args*/);
virtual ~Rectangle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // RECTANGLE_HPP_INCLUDED
and Circle.hpp
#ifndef CIRCLE_HPP_INCLUDED
#define CIRCLE_HPP_INCLUDED
#include "Object.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include <iostream>
#include <cstdlib>
class Object; // <--- without this line *expected class-name before '{' token*, but with it *invalid use of incomplete type 'class Object'*
class Circle: public Object
{
public:
Circle(/*args*/);
virtual ~Circle();
//some functions
void update();
void draw();
private:
//some members
};
#endif // CIRCLE_HPP_INCLUDED
I don't know what to do, I tried changing the order of all includes, removing stuff and adding it, i searched everywhere but there was no good solution. Thank you for all answers
just insert ' #include "Object.hpp" ' in Circle.hpp and Rectangle.hpp and any other file like them that is using Object class:
#include "ObjectParent.hpp"
#include <SFML/Graphics.hpp>
#include "../Data/Data.hpp"
#include "../MainClass.hpp"
#include "Object.hpp" // <----------
#include <iostream>
#include <cstdlib>

Swarmed with identifier errors

I've been programming a Monopoly game for a final project. So I thought I was on a roll, and that I had everything figured out with my psuedocode. But, it seems I forgot how to deal with includes properly, I know that is the issue since I was able to refine it to that point, but I'm not sure how to fix it.
In this super stripped down version of my code I have three .h files "Space.h" which is an abstract/virtual class which has to be inherited by a variety of different spaces that can appear on a typical Monopoly board: properties, jail, taxes, Chance, Community Chest, etc. The function that has to be inherited is run(Player&) which is what is "run" when you land on that particular space on the board, all functions that use run use a player passed by argument.
#pragma once
#include <string>
#include "Player.h"
class Space
{
public:
virtual void run(Player&) = 0;
};
My second .h file is the "Property.h" this inherits from Space
#pragma once
#include "Space.h"
class Property : Space
{
public:
void run(Player&) override;
int i{ 0 };
};
Lastly I have the "Player.h" which has two variables a name and a vector of properties it owns.
#pragma once
#include <string>
#include <vector>
#include "Property.h"
class Player
{
public:
std::string name{ "foo" };
void addProperty(Property p);
private:
std::vector <Property> ownedProperties;
};
Here's a very basic Property implementation
#include "Property.h"
#include <iostream>
void Property::run(Player & p)
{
std::cout << p.name;
}
Player implementation
#include "Player.h"
#include <iostream>
void Player::addProperty(Property p)
{
ownedProperties.push_back(p);
}
And finally main
#include "Player.h"
#include "Space.h"
#include "Property.h"
int main()
{
Player p{};
Property prop{};
prop.run(p);
system("pause");
}
Every time this is run I get a slew of errors, I'm sure it's got to do something with the circular include logic, with player including property, and property including space, which includes player. But, I don't see a workaround considering #include is needed to know how everything is defined isn't? Or are these errors referring to something else?
You have a circular include problem. Player includes Property which includes Space which includes Player again.
You can break the circle by not including Player.h in Space.h and only forward declare the class
#pragma once
class Player;
class Space
{
public:
virtual void run(Player&) = 0;
};

Arduino: Inheritance and arrays of pointer subclasses

This is problem #2 from this previous question:
Inheritance in Arduino Code
Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.
This is my "Board" class I have so far, that contains multiple child elements:
Board.h:
#ifndef Board_h
#define Board_h
#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>
#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"
class Board
{
public:
Board();
void draw(double* matrix);
private:
Marble marble;
//std::vector<Actor> children;
Actor* children[2];
};
#endif
Board.cpp:
#include "Arduino.h"
#include "Board.h"
#include <math.h>
#include <iterator>
#include <vector>
Board::Board()
{
}
void Board::create(double* _matrix, int _cols, int _rows) {
Marble *marble = new Marble();
Wall wall;
children[0] = marble;
//children.push_back(marble);
//children.push_back(wall);
}
void Board::draw(double* matrix) {
Serial.println("board draw");
children[0]->speak();
}
In my "loop" function I am calling
board.draw(matrix);
which results in some nutty Serial code being written out.
Clearly I am not understanding the ins and outs of pointers in arrays in classes here.
You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.
class Actor
{
public:
Actor();
virtual void speak(); // virtual
private:
};

Undeclared Identifier vector of pointers to objects

Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".

Incomplete type C++

I get the following error when I try to execute this code segment : "Menu does not name a type".I know its something to do with the circular references, but for the life of me I can't figure out what. Also, menu, go, and manager are repeatedly giving errors. The code segments are posted below :
#ifndef GO__H
#define GO__H
#include <SDL.h>
#include <iostream>
#include <string>
using std::cout; using std::endl;
using std::string;
#include "ioManager.h"
#include "gui.h"
#include "clock.h"
#include "menu.h"
//class Menu;
class Go {
public:
Go ();
void play();
private:
SDL_Surface *screen;
Gui gui;
Menu menu;
void drawBackground() const;
Go(const Go&);
Go& operator=(const Go&);
};
#endif
Here's Menu :
#ifndef MENU_H
#define MENU_H
#include <SDL.h>
#include <iostream>
#include "ioManager.h"
#include "gui.h"
#include "clock.h"
#include "manager.h"
class Menu {
public:
Menu ();
void play();
private:
const Clock& clock;
bool env;
SDL_Surface *screen;
Gui gui;
Manager mng;
void drawBackground() const;
Menu(const Menu&);
Menu& operator=(const Menu&);
};
#endif
Manager :
#ifndef MANAG_H
#define MANAG_H
#include "go.h"
class Manager {
Go go;
//other code
}
Can you see where the problem is? Error message:
In file included from go.h:13:0,
from manager.h:33,
from manager.cpp:2:
menu.h:28:11: error: field ‘mng’ has incomplete type
manager.h includes go.h which includes menu.h which includes manager.h ...
The class Menu is being defined before it ever gets to the definition of class Manager.
However, class Menu needs a Manager but since the compiler doesn't know about Manager yet it doesn't know how big to make it.
You could forward declare class Manager and make the mng member of Menu a pointer or reference:
class Manager;
class Menu {
...
Manager* mng;
// or this:
//Manager& mng;
...
Here's a good explanation of circular references and how to fix them.
It appears you are missing the semicolon at the end of the declaration of your Manager class in manger.h.
You are also missing the #endif to close your include guard.