How can I pass a c++ object as a parameter to function? - c++

I've got three classes in a project I'm working on called Pixel, custFrame, and FrameHolder.
My custFrame class header is like so:
#pragma once
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "Pixel.h"
#ifndef CUSTFRAME_H
#define CUSTFRAME_H
class custFrame
{
public:
custFrame();
void addPixel(Pixel pix);
void setWidth(int width);
void setHeight(int height);
int getWidth();
int getHeight();
int getPixelSize();
Pixel getPixel(int count);
private:
std::vector<Pixel> pixels;
int Height;
int Width;
};
#endif
and my FrameHolder class header is like so:
#pragma once
//Hold all captured frames containing data
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "custFrame.h"
#include "Pixel.h"
#ifndef FRAMEHOLDER_H
#define FRAMEHOLDER_H
class FrameHolder {
public:
FrameHolder();
static FrameHolder* instance();
void addFrame(IDeckLinkVideoFrame* fram);
void calibrate(custFrame fram);
int numFrames();
void setWidth(int width);
void setHeight(int height);
static FrameHolder *inst;
bool calibrating;
int getHeight();
int getWidth();
bool isCalibrating();
private:
//Member variables
int Width;
int Height;
std::vector<IDeckLinkVideoFrame *>frames;
};
#endif
In my FrameHolder class passing a custFrame object to a function to store that frame in the object does not seem to work. I get a compiler error ("syntax error: identifier 'custFrame' line 24"). However in my custFrame class, passing a Pixel object to be stored as part of a frame works wonderfully. Am I missing something? I've seen this post but it didn't help much.

The problem is caused by the presence of
#include "FrameHolder.h"
in both the .h files. Because of that, the definition of custFrame is not seen before the definition of FrameHolder.

Passing by pointers/references is probably what you should be doing here. As for the syntax error, it might be that the custFrame class is not declared properly when you include it in the header.

Related

Error constructing an Object from a custom C++ Library in Arduino

I attempted to compile this Arduino sketch with a custom library in it (jtaServoController), and the Arduino IDE claims that my constructor in the Arduino sketch was written incorrectly (more precisely, 'jtaServoControllerFour' does not name a type).
#include <jtaServoController.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
#define SERVOMIN 0
#define SERVOMAX 600
jtaServoControllerFour pointerFinger(1,2,3,4); //line in question
void setup() {
Serial.begin(9600);
pwm1.begin();
pwm1.setPWMFreq(60);
}
void loop() {
pointerFinger.jtaSetPWMFour(300,400,500,600);
}
My Question is whether the line in question actually is written wrong or is there an issue in another part of my code?-probably in the library itself which I have below. (btw I found the information for constructing an object in the Arduino tutorial on libraries).
These are the Header and .cpp files respectively:
#ifndef jtaServoController_h
#define jtaServoController_h
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
#include "Arduino.h"
class jtaServoControllerFour
{
public:
jtaServoControllerFour(int servo1, int servo2, int servo3, int servo4);
void jtaSetPWMFour(unsigned int servo41, unsigned int servo42, unsigned int servo43, unsigned int servo44);
private:
int _servoOne;
int _servoTwo;
int _servoThree;
int _servoFour;
};
#endif
CPP file
#include "Arduino.h"
#include "jtaServoController.h"
#include "Adafruit_PWMServoDriver.h"
#include "Wire.h"
jtaServoControllerFour::jtaServoControllerFour(int servoOne, int servoTwo, int servoThree, int servoFour)
{
_servoOne = servoOne;
_servoTwo = servoTwo;
_servoThree = servoThree;
_servoFour = servoFour;
}
void jtaServoControllerFour::jtaSetPWMFour(int servoFourOne, int servoFourTwo, int servoFourThree, int servoFourFour)
{
pwm1.setPWM(_servo1, 0, servoFourOne);
pwm1.setPWM(_servo2, 0, servoFourTwo);
pwm1.setPWM(_servo3, 0, servoFourThree);
pwm1.setPWM(_servo4, 0, servoFourFour);
return;
}
#include <jtaServoController.h>
You may need to use the following instead (double quotes):
#include "jtaServoController.h"
And/or check the path to your header file.
See here for the difference between the two.

extern vector created in constructor

I have a class Team and a class Ball and I create a vector in constructor of Team that is filled with objects of another class called Player. So I want to use this vector in the class Ball but even though I define it as extern (public) compiler keeps telling me that I have undefined reference to team that is my vector. Here follows the code of Team.cpp and Ball.cpp
Team.h
#define TEAM_H
#include <iostream>
#include <vector>
#include "Player.h"
using namespace std;
extern vector<Player> team;
class Team {
public:
Team();
void fillTeamVector(vector<Player>&);
private:
string teamName;
int faults;
int passes;
int goals;
};
#endif // TEAM_H
Team.cpp
#include "Team.h"
#include <vector>
#include <iostream>
#include "Player.h"
#include "Attacker.h"
#include "Defender.h"
#include "Ball.h"
Team::Team()
{
extern vector<Player> team;
fillTeamVector(team);
}
void Team::fillTeamVector(vector<Player>& team){
// do stuff and store them on vector team
}
And here follows the code for Ball.h note that I commented all the methods that don't affect the problem.
#ifndef BALL_H
#define BALL_H
#include "Player.h"
class Ball
{
public:
Ball();
Player* current;
Player* previous;
/*void setX_ball(int);
int getX_ball() const;
void setY_ball(int);
int getY_ball() const;*/
void assign();
//void changeCurrentToPrevious();
//void changeNextToCurrent(Player*);
private:
int X_ball;
int Y_ball;
};
#endif // BALL_H
Here is the code for Ball.cpp note that in method assign if I create a new (and obviously different vector of Player named team it will compile correctly)
#include "Ball.h"
#include "Team.h"
#include "Player.h"
extern vector<Player> team;
Ball::Ball()
: X_ball(2),
Y_ball(5)
{
current = NULL;
previous = NULL;
}
void Ball::assign(){
//vector<Player> team;
int x;
int y;
x=(team[0].getX())-X_ball;
y=(team[0].getY())-Y_ball;
int min=x+y;
int k=0;
for (int i=1; i<team.size(); i++){
x=(team[i].getX())-X_ball;
y=(team[i].getY())-Y_ball;
int sum=x+y;
if(sum<min){
k=i;
}
}
current = &team[k];
}
By doing
extern vector<Player> team;
you only declare the variable.
In one source file you must actually define the variable:
vector<Player> team;
Note the lack of extern in the definition.
Also note that this has to be done in the global scope, since you want a global variable. So it has to be defined outside of any functions or classes or namespaces.

C++ class name does not name a type

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include "Setup.h"
#include "mainCharacter.h"
class Main {
public:
Main(int pScreenWidth, int pScreenHeight);
~Main();
void gameLoop();
private:
bool quit;
Setup* setup;
mainCharacter* tom;
int screenWidth;
int screenHeight;
int mouseX;
int mouseY;
};
#endif // MAIN_H_INCLUDED
I saw the other topics on this, but the error im getting is not of the same type, how can it tell me that
Setup* setup;
mainCharacter* tom;
does not name a type when the includes are above ?
im using code::blocks if that matters.
EDIT://////////////////////
This is the Setup header:
#ifndef SETUP_H_INCLUDED
#define SETUP_H_INCLUDED
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include "Main.h"
#include "Sprite.h"
class Setup {
public:
Setup(int screenWidth, int screenHeight);
~Setup();
SDL_Renderer* getRenderer();
SDL_Event* getEvent();
private:
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event* event;
};
#endif // SETUP_H_INCLUDED
and this is the mainCharacter header:
#ifndef MAINCHARACTER_H_INCLUDED
#define MAINCHARACTER_H_INCLUDED
#include "Sprite.h"
#include "Setup.h"
#include <cmath>
class mainCharacter {
public:
mainCharacter(Setup* pSetup, int* pMouseX, int* pMouseY);
~mainCharacter();
double getDistance(int x1, int y1, int x2, int y2);
void update();
void draw();
private:
Sprite* shagy;
///some variables are here/////
Setup* setup;
};
#endif // MAINCHARACTER_H_INCLUDED
there are Setup.cpp and mainCharacter.cpp files and they got a long code written on them, the only thing i inclue in setup.cpp is setup.h and maincharacter.cpp i only include maincharacter.h
#ifndef SETUP_H_INCLUDED
#define SETUP_H_INCLUDED
Make sure you aren't defining SETUP_H_INCLUDED somewhere else, this is the only reason i see.
Also it's possible that your %INCLUDE% (include directories) has another Setup.h somewhere, which does not define Setup class, and your Setup.h is not loaded.

C++ does not support default-int [closed]

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 7 years ago.
Improve this question
I am getting an error on my private variables Player_player; and Level _level; it is telling me that it is missing the type specifier and im not sure why this is. I have made classes for both Level and Player, so shouldnt I be able to use Player and Level to specify the variable type?
thanks for the help,
mike
//GameSystem.h
#pragma once
#include "Player.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#
//Player.h
#pragma once
#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
Your GameSystem.h file has the line:
#include "Player.h"
Your Player.h file has the line:
#include "GameSystem.h"
This can't be good. And besides the Player class declaration in the header file doesn't use anything from GameSystem.h, so remove the GameSystem.h from the header file (I recommend removing all the #include that don't resolve anything in Player.h header file).
Edit 1:
I changed the header files and made them use Include Guards. I don't get any errors.
Player.hpp:
#ifndef PLAYER_HPP
#define PLAYER_HPP
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
#endif // PLAYER_HPP
GameSystem.hpp:
#ifndef GSYSTEM_HPP
#define GSYSTEM_HPP
#include "Player.hpp"
#include "Level.hpp"
#include <string>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#endif // GSYSTEM_HPP
I changed the filename extensions to help distinguish between C language header files (.h) and C++ language header files (.hpp).
I also simplified the header files by remove unused include files.
You have a dependency problem here, there are two solutions.
First, Player.h isn't actually dependent on GameSystem.h, so don't include GameSystem.h in Player.h.
//Player.h
#pragma once
//Remove GameSystem.h
//#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
If, for some reason, you DO need the GameSystem class declaration in the Player class, just do this:
//Player.h
#pragma once
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
//Declare the class
class GameSystem;
You will need to include the full header file in the .cpp file, but you don't actually need the full class definition to use a class in another classes definition (actually, GameSystem.h doesn't even need to include Player.h, it could just declare but not define the Player class)

Classes as parameters error

In Weapon.h, when I try and take a class 'Entity*' as a parameter, it says "Syntax error: identifier 'Entity'" when I compile. Additionally when I roll over the text 'target', Visual C++ Express 2010 gives me the text " *target". The Entity class is fine and I'm pretty sure it's included correctly.
(I won't post Player.h as it's unnecessary - see Library.h - but it has a header guard and includes Entity.h)
Library.h:
#ifndef _LIBRARY_
#define _LIBRARY_
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>
//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);
std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);
#endif //__LIBRARY__
Weapon.h:
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Shopable.h"
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Shopable(c,n),Damage(d){}
std::string getDesc() const{
return getName()+"\t"+tostring(Damage)+"\t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
#endif
Shopable.h:
#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_
#include "Library.h"
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
Shopable(int c, std::string n):Cost(c),Name(n){}
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
};
#endif
Entity.h:
#ifndef _ENTITY_
#define _ENTITY_
#include "Library.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
class Entity{
public:
void printStats() const;
void heal(double health);
std::string name;
protected:
//player stats
double str; //strength
double wis; //wisdom
double ref; //reflex
double hp; //health points
double maxHp; //maximum health points
double i; //initiative
double inte; //intelligence
double c; //courage
int gold; //gold
int xp; //experience
int ap; //armour points
int wd; //weapon damage
int lvl; //level
int sp; //skill points
Weapon* weapon;//weapon
Armour* cArmour;//current armour
};
#endif
In C++, classes must be declared before they are referenced. You are #include-ing Weapon.h in Entity.h, but at that point, the compiler doesn't know about the existence of class Entity.
You will either need to change the order in which things are declared, or add a forward declaration "above" class Weapon. It can simply be:
class Entity;
That tells the compiler that there is such a name as Entity. However, it doesn't tell it anything about what members it has, so you can't actually do anything with it, other than declare variables of Entity * and Entity &, and pass them around.
Your headers include each other because your classes refer to each other. (But your compiler doesn't suffer from a stackoverflow because of your include guards - that's a good thing!)
You should arrange your header files hierarchically, ie there are files at the 'top' which #include nothing and files 'below' which include some of the top ones and so-on down the hierarchy. But at no point should there be 'loops'.
In order to break your loops in your code, any classes that refer to each other should forward declare any mutual dependencies and only refer to dependency names and not their members.
e.g.
Entity.h
class Weapon;
class Entity{
...
Weapon* weapon;
};
Weapon.h
class Entity;
class Weapon{
...
int DamageTarget(Entity* target);
};
Notice how Weapon.h only refers to Entity*.
You will need to define int Weapon::DamageTarget(Entity* target) in Weapon.cpp
#include <entity.h>
Or forward-declare only in the header
class Entity;
This makes compilation a bit faster (you still need to include it to use in the implementation).
Weapon.h doesn't #include Entity.h, or anything that recursively includes it. Therefore, it doesn't know about the class Entity.