Currently I have a tic tac toe board "tttBoard" with a constructor
tttBoard::tttBoard() {
isX = true;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
gBoard[x][y]=sEmp;
}
}
}
That should create a new board and fill it with the enum sEmp. isX is a boolean which marks that the first player moves first. Despite having #include "tttBoard.h" and (I believe) having the constructor in that header file (below), I've run across the same errors over and over:
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
tttBoard.h
#ifndef tttBoard
#define tttBoard
class tttBoard {
public:
tttBoard();
void Draw();
void Move(int x, int y);
char* getValue(int x, int y);
private:
enum sVal {
sEmp,
sX,
sO
};
sVal gBoard[3][3];
bool isX;
}
#endif
#ifndef tttBoard
#define tttBoard
class tttBoard {
That's not a proper include guard. You're defining tttBoard as an empty symbol and then using the same name for the class.
#ifndef TTT_BOARD_H
#define TTT_BOARD_H
class tttBoard {
// stuff
};
#endif
Related
I've got 2 classes, casilla.cpp and casilla.h.
In the cpp one I get the error of class redefined, and in .h "there's a previous definition of the classs casilla. I've searched for it in the internet, but not even putting casilla:: before one or putting the headers work. Here's the code:
Casilla.h:
#ifndef CASILLA_H_
#define CASILLA_H_
using namespace std;
class Casilla { //previous definition of ‘class Casilla’
public:
casilla(); //ISO C++ forbids declaration of ‘casilla’ with no type [-fpermissive]
virtual ~casilla(); //expected class-name before ‘(’ token
void SetNumeroCasilla (int _numero);
};
/* namespace std */
#endif /* CASILLA_H_ */
Casilla.cpp:
#include "Casilla.h"
#include "Tablero.h"
using namespace std;
#include <iostream>
#include <iomanip>
class Casilla //errors :Multiple markers at this line
- redefinition of ‘class Casilla’
- Line breakpoint: Casilla.cpp [line:
17]
{
int fila;
int columna;
int numero;
public:
// default constructor
Casilla::Casilla()
: fila(-1)
, columna(-1)
, numero(0)
{ }
int GetNumero() {return numero;}
void SetCasillaPosition (int _fila, int _columna) //set a cell position
{
fila = _fila;
columna = _columna;
}
void SetNumeroCasilla (int _numero) //set a cell value
{
numero = _numero;
}
void SetCasillaFull (int _fila, int _columna, int _numero) //set a cell position and value
{
fila = _fila;
columna = _columna;
numero = _numero;
}
};
Just changed the code with new errors shown. The redefined error persists, what did I do wrong?
In casilla.cpp, you're redefining casilla... class casilla { .. }; is a class definition, and you have it twice: once in your header and once in your cpp. Hence, the redefinition error.
All you need to do in the .cpp is provide definitions for the class methods you declared in your .h:
#include "Casilla.h"
// other includes
// define the default constructor:
casilla::casilla()
: fila(-1)
, columna(-1)
, numero(0)
{ }
// define this function
void casilla::SetNumeroCasilla (int _numero)
{
// something
}
I have been having issues with Visual Studio 2013. I can't explain why its happening. The compile issue is shown in the pictures below.
I have one class "PhysicsEngine.cpp/h" that use the class "RigidBody.cpp/h" just fine. Once I attempt to use the RigidBody class in my Character class header, it fails to see the RigidBody class identifier. I have included "RigidBody.h" in my file, and even tried prototyping the class. Interesting enough when I delete the "RigidBody.h" header inclusion, it spits out even more errors, leading me to believe it is reading it. Is there something I'm doing wrong?
Here is the output I received as an error:
1>------ Rebuild All started: Project: SideScroller, Configuration: Debug Win32 ------
1> WorldChunkManager.cpp
1> World.cpp
1> Vector.cpp
1>e:(directory)\vector.cpp(41): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>e:(directory)\vector.cpp(48): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1> Tile.cpp
1> RigidBody.cpp
1>e:(directory)\character.h(21): error C2146: syntax error : missing ';' before identifier 'pos'
1>e:(directory)\character.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:(directory)\character.h(21): error C2143: syntax error : missing ';' before '='
1>e:(directory)\character.h(21): error C2238: unexpected token(s) preceding ';'
Here is the code for the RigidBody.h
#ifndef RH
#define RH
#include "Vector.h"
#include "World.h"
#include <cmath>
#include <list>
class RigidBody
{
private:
int id;
float gravityFactor;
float airResistFactor;
float mass;
float elasticity;
Vector pos = Vector(0, 0);
int width;
int height;
Vector velocity = Vector(0, 0);
std::list<Vector>* additiveVelocities;
std::list<Vector>* forces;
public:
RigidBody(int x, int y, int w, int h, float mass, float elasticity, float gravityFactor, float airResistFactor);
~RigidBody();
static int rigidBodyCount;
//Get/Set
int getID();
double getX();
double getY();
float getGravFact();
float getAirFact();
float getMass();
float getElast();
Vector getPos();
Vector getVelocity();
std::list<Vector>* getadditiveVelocities();
std::list<Vector>* getForces();
void setX(double x);
void setY(double y);
void setVelocity(Vector& vec);
void setPos(Vector& vec);
//Interactino Functions
void addForce(Vector force);
void addVelocity(Vector velocity);
};
#endif
Here is the code that will not work (Character.h):
#ifndef CHARACTER_H
#define CHARACTER_H
#include "SDL.h"
#include "Camera.h"
#include "InputManager.h"
#include "Vector.h"
#include "RigidBody.h"
//TEMP DIM
const int CHAR_H = 64;
const int CHAR_W = 12;
class Character
{
private:
double speed;
//Vector pos = Vector(0, 0);
RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);
InputManager* inputPtr;
public:
Character(int x, int y, InputManager* inputPtr);
void getXY(int* dest);
void getChXY(int* dest);
void getCenterXY(int* dest);
//Update
void update(long double last);
bool isFloor();
//Draw
void draw(SDL_Surface* screen, Camera* camPtr);
};
#endif
Let me know if there is anything additional you could need to find the problem!!! I don't want to overload the page with potentially irreverent information. Thank you!
I think the problem with this line isn't that it's failing to find an identifier:
RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);
Rather, it's that you're trying to do an illegal initialization, in a C#-type way. You can't just assign a value to a class member as part of its declaration like that in C++. You need to assign the initial value in the constructor, or in an initializer attached to the constructor.
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::.
I am trying to create a vector of custom objects defined in a header file and then initialize them in the actual cpp file. I'm getting the following errors in Visual Studio:
error C2976: 'std::vector' : too few template arguments
error C2065: 'Particle' : undeclared identifier
error C2059: syntax error : '>'
In the code below, the vector is defined in Explosion.h.
Particle.h:
#pragma once
class Particle : public sf::CircleShape {
public:
float speed;
bool alive;
float vx;
float vy;
Particle(float x, float y, float vx, float vy, sf::Color color);
~Particle();
};
Particle.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
Particle::Particle(float x, float y, float vx, float vy, sf::Color color) {
// Inherited
this->setPosition(x, y);
this->setRadius(5);
this->setFillColor(color);
// Player Defined Variables
this->speed = (float).05;
this->alive = true;
this->vx = vx;
this->vy = vy;
}
Particle::~Particle() {
}
Explosion.h:
static const int NUM_PARTICLES = 6;
#pragma once
class Explosion {
public:
std::vector<Particle*> particles;
bool alive;
Explosion();
~Explosion();
};
Explosion.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
#include "Explosion.h"
Explosion::Explosion() {
this->alive = true;
// Add Particles to vector
for (int i = 0; i < NUM_PARTICLES; i++) {
this->particles.push_back(new Particle(0, 0, 0, 0, sf::Color::Red));
}
}
Explosion::~Explosion() {
}
I'm sure there is something fundamentally wrong here since C++ is fairly new to me.
You need to tell Explosion.h what a Particle is.
In this case, Explosion.h is using Particle*, so a forward declartion will suffice.
Explosion.h
class Particle; // forward declaration of Particle
class Explosion {
// ...
};
You could also simply #include "Particle.h, however as your projects increase using forward declarations (instead of direct includes) can significantly reduce your build times.
I'm trying to write a singleton class to hold the state of inputs from the user (mouse/keyboard data). The SDL API returns keyboard data as Uint8 pointer array, however, why I try to create the Uint8 pointer, I get these errors at the line w/ the uint8:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I've used Uint8 as a data type without defining it before, so I'm not sure what is causing the issue here. Here is my code:
class InputState {
public:
InputState()
{};
~InputState()
{};
static InputState *getInputState(void)
{
static InputState *state = new InputState();
return state;
};
public:
Uint8 *keys;
struct MouseState
{
int LeftButtonDown;
int RightButtonDown;
int MiddleButtonDown;
int x;
int y;
MouseState ()
{
LeftButtonDown = 0;
RightButtonDown = 0;
MiddleButtonDown = 0;
x = 0;
y = 0;
}
};
MouseState *mouseState;
};
The type Uint8 is a typedef that is defined in one of the SDL header. If you want to use it, you need to include the SDL.h header in your file.
// You need this include if you want to use SDL typedefs
#include <SDL.h>
class InputState {
public:
InputState()
{};
~InputState()
{};
// ...
public:
Uint8 *keys;
// ...
};