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
}
Related
I have a C++ program with an undefined reference error on the call of a method in a class. As near as I can tell, the method is public and I'm calling it with a parameter declared the same as the method definition, but eclipse/gcc tells me it is undefined. I'm not used to C++, can someone please tell me what's wrong with the definition?
The main class:
#include <iostream>
using namespace std;
#include "AttenuationConfigurationTable.h"
int main()
{
flash_entry_struct flash_array[] = { { 10, 20, 30, 40 }, { 50, 60, 70, 80 }, { -1, -1, -1, -1 } };
// note: no undefined reference or any other error for the line with the class
AttenuationConfigurationTable attConfigTable;
// error appears for next line: undefined reference to ...load_attenuation_...
attConfigTable.load_attenuation_calibration_table_from_flash(flash_array);
return 0;
}
The class file:
#include "AttenuationConfigurationTable.h"
#include "flashEntryStruct.h"
AttenuationConfigurationTable::AttenuationConfigurationTable() { }
AttenuationConfigurationTable::~AttenuationConfigurationTable() { }
class Attenuation_configuration_table
{
struct attenuation_voltages_struct
{
float att_value;
float v1;
float v2;
} ;
struct frequency_tables_struct
{
int frequency;
attenuation_voltages_struct attenuation_voltages[100];
int voltages_count = 0;
} ;
frequency_tables_struct _frequency_tables[42];
public:
/************************************************************************/
/* load the table in this object from the given flash memory address */
/************************************************************************/
void load_attenuation_calibration_table_from_flash(flash_entry_struct memory_address[])
{
// bunch of logic here...
}
};
The h file for the class:
#ifndef ATTENUATIONCONFIGURATIONTABLE_H_
#define ATTENUATIONCONFIGURATIONTABLE_H_
#include "flashEntryStruct.h"
class AttenuationConfigurationTable
{
public:
AttenuationConfigurationTable();
virtual ~AttenuationConfigurationTable();
void load_attenuation_calibration_table_from_flash(flash_entry_struct flash_memory_address[]);
};
#endif /* ATTENUATIONCONFIGURATIONTABLE_H_ */
And, just for completeness, the h file defining the parameter structure:
#ifndef FLASHENTRYSTRUCT_H_
#define FLASHENTRYSTRUCT_H_
struct flash_entry_struct
{
uint16_t frequency;
uint16_t scaled_db;
int8_t v1_byte;
int8_t v2_byte;
} ;
#endif /* FLASHENTRYSTRUCT_H_ */
EDIT: the error message itself:
Invoking: Cygwin C++ Linker
g++ -o "HelloCPP.exe" ./src/AttenuationConfigurationTable.o ./src/Hello2.o ./src/HelloCPP.o
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: ./src/HelloCPP.o:/cygdrive/c/Users/ralph/files/programming/workspaces/HelloCPP/HelloCPP/Debug/../src/HelloCPP.cpp:15: undefined reference to `AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(flash_entry_struct*)'
collect2: error: ld returned 1 exit status
make: *** [makefile:58: HelloCPP.exe] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
You declare AttenuationConfigurationTable in the header file with the load_attenuation_calibration_table_from_flash function, but then the function with the same name in the implementation file is inside the definition for another class, Attenuation_configuration_table.
Take the implementation for load_attenuation_calibration_table_from_flash out of the class definition for Attenuation_configuration_table in your cpp file, and define it instead as
void AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(/* ... */) {
// ...
}
This is the same syntax already used for the constructor and destructor in that file.
In the end, your header should look something like this:
class AttenuationConfigurationTable
{
public:
AttenuationConfigurationTable();
virtual ~AttenuationConfigurationTable();
void load_attenuation_calibration_table_from_flash(flash_entry_struct flash_memory_address[]);
private:
struct attenuation_voltages_struct
{
float att_value;
float v1;
float v2;
} ;
struct frequency_tables_struct
{
int frequency;
attenuation_voltages_struct attenuation_voltages[100];
int voltages_count;
} ;
frequency_tables_struct _frequency_tables[42];
};
and your cpp file should look something like this:
AttenuationConfigurationTable::AttenuationConfigurationTable() { }
AttenuationConfigurationTable::~AttenuationConfigurationTable() { }
void AttenuationConfigurationTable::load_attenuation_calibration_table_from_flash(/* params */) {
// body
}
I'm writing a c++ stack and queue implementation program, I finished the stack part, but when compiling I'm getting these errors
arrayListImp.cpp:18:19: error: expected unqualified-id
arrayList[++top]= x;
^
arrayListImp.cpp:28:13: error: 'arrayList' does not refer to a value
itemPoped=arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:35:9: error: 'arrayList' does not refer to a value
return arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:46:9: error: 'arrayList' does not refer to a value
cout<<arrayList[i]<<endl;
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
4 errors generated.
Here is the header file
#ifndef ARRAYLIST_H
class arrayList{
public:
arrayList();
static const int maxSize = 10;
int array[10];
};
class stack : public arrayList{
public:
stack();
void push(int x);
void pop();
int Top();
int isEmpty();
void print();
int x;
int top;
int itemPoped;
int i;
};
#define ARRAYLIST_H
#endif
arrayListImp.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
//Stack implementation
stack::stack(){
top = -1;
}
void stack::push(int x){
if (top == maxSize -1){
cout<<"Stack overflow"<<endl;
}
else{
arrayList[++top]= x;
cout<<x<<", is pushed on to the stack"<<endl;
}
}
void stack::pop(){
if (top == -1){
cout<<"Stack underflow"<<endl;
}
else{
itemPoped=arrayList[top];
top--;
cout<<itemPoped<<", is poped from the stack"<<endl;
}
}
int stack::Top(){
return arrayList[top];
}
int stack::isEmpty(){
if (top == -1) return 1;
return 0;
}
void stack::print(){
cout<<"Stack: "<<endl;
for (i = 0; i<=top; i++){
cout<<arrayList[i]<<endl;
}
}
arrayListUse.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
int main()
{
//Stack testing
stack S;
S.push(1);S.print();
S.push(2);S.print();
S.push(3);S.print();
S.pop();S.print();
S.push(4);S.print();
//Queue testing
return 0;
}
Can you please point out to what I'm doing wrong here?
You should just read your error messages.
You should use array instead of arrayList, which is the name of the class. So just refer to the variable instead.
The error message you got is something like
test.cpp: In member function ‘void stack::push(int)’:
test.cpp:44:18: error: expected unqualified-id before ‘[’ token
arrayList[++top]= x;
^
When you check the line, you immediately see what is wrong there.
You declare a constructor arrayList::arrayList(), but you do not define it. Either you can drop the declaration, or you should implement it in the cpp-file.
arrayList::arrayList() {
// do some initialization
}
The error message you got is something like
/tmp/cc4y06YN.o:test.cpp:function stack::stack(): error: undefined reference to 'arrayList::arrayList()'
The code could compile, but it did not link. So all declarations may be correct, but a symbol was missing. This is usually the case when you declared something you referred to, but you never defined it.
You always have written
arrayList[...]
what is the name of your class but reading the code it seems like you wanted to write
array[...]
which would access the data.
Here is my header file. Im trying to compile this but i got an error at void init(TipMatrice tip) "initializing argument 1 of 'void graf_matrice::init(graf_matrice::TipMatrice)' " which redirects me to the main with another conversion error. I think its something wrong with my enum.
#ifndef GRAF_MATRICE_H
#define GRAF_MATRICE_H
class graf_matrice
{
public:
static const int MaxN = 50; // numarul maxim de noduri
static const int Infinit = 99999; // nu exista drum intre noduri
enum TipMatrice
{
MatriceAdiacenta = 1,
MatriceCosturi = 2
};
public:
graf_matrice();
void init(TipMatrice tip); //iniţializare graf
};
#endif // GRAF_MATRICE_H
Here is my main. I got an error at g.init(1) "invalid conversion from int to graf_matrice::TipMatrice" .
#include <iostream>
#include"graf_matrice.h"
using namespace std;
int main()
{
graf_matrice g;
g.init(1);
return 0;
}
Could you please help me with this cuz i dont know what is wrong. Sorry for bad language im beginner.
As you've defined an enum, you'd better use its values properly:
g.init(graf_matrice::MatriceAdiacenta);
Explanation: the enumerators (here MatriceAdiacenta) in an unscoped enum (here TipMatrice) are defined in the scope containing the enum (here the class graf_matrice). This is why you have to indicate the scope with ::
Alternatively, if you really need to convert from an int, you could still do it explicitly:
g.init(static_cast<graf_matrice::TipMatrice>(1));
I'm using the Arduino IDE 1.0.5-r2 and trying to create a class with two member variables, _pinA and _pinB. When I call the constructor from my Arduino sketch, I get this error:
RotaryEncoderReader.cpp:6: error: request for member '_pinB' in 'this', which is of non-class type 'RotaryEncoderReader* const'
The constructor can be called from a regular C++ files compiled using GCC, and there are no errors. Am I missing something about how to use a class constructor with an Arduino?
Here is the class header:
#ifndef RotaryEncoderReader_h
#define RotaryEncoderReader_h
#include "Arduino.h"
class RotaryEncoderReader {
private:
int _pinA;
int _pinB;
volatile long encoderPos;
public:
RotaryEncoderReader( int newPinA, int newPinB );
void doEncoderA();
void doEncoderB();
long getPosition();
};
#endif
Here's the implementation:
#include "RotaryEncoderReader.h"
RotaryEncoderReader::RotaryEncoderReader( int newPinA, int newPinB )
: _pinA(newPinA),
_pinB(newPinB),
encoderPos(0)
{
}
void RotaryEncoderReader::doEncoderA()
{
//Irrelevant
}
void RotaryEncoderReader::doEncoderB()
{
//Irrelevant
}
long RotaryEncoderReader::getPosition()
{
return _pinA + _pinB;
}
And here's the Arduino sketch:
#include <RotaryEncoderReader.h>
int pinA = 2;
int pinB = 3;
RotaryEncoderReader reader(pinA, pinB);
void setup()
{
}
void loop()
{
}
I have an error that goes like this
In file included from Level.hpp:12,
from main.cpp:4:
Corridor.hpp: In method `void Game::Corridor::update()':
Corridor.hpp:41: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:42: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:43: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor.hpp:44: invalid use of undefined type `class Game::Level'
Corridor.hpp:13: forward declaration of `class Game::Level'
Corridor and Level are ...
// Corridor.hpp
#ifndef GAME_CORRIDOR_HPP
#define GAME_CORRIDOR_HPP
#include <Moot/Math.hpp>
//#include <Level.hpp>
#include <GameWindow.hpp>
namespace Game
{
class Level; // <-- LINE 13
class Corridor
{
static const unsigned int defaultLevelDepth = 800;
Moot::Math::Vector3D wp1, wp2, wp3, wp4;
Moot::Math::Vector2D sp1, sp2, sp3, sp4;
Level * p_level;
public:
Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint)
{
wp1 = setFirstPoint;
wp2 = setSecondPoint;
wp3 = setFirstPoint;
wp3.z += defaultLevelDepth;
wp4 = setSecondPoint;
wp4.z += defaultLevelDepth;
}
void update() {
sp1 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp1); // <- LINE 41 etc.
sp2 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp2);
sp3 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp3);
sp4 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp4);
//p_level->getLevelCamera();
}
void draw()//const
{
Moot::Color tempColor;
windowInstance().graphics().drawQuad( sp1.x, sp1.y, tempColor,
sp2.x,sp2.y, tempColor,
sp3.x, sp3.y, tempColor,
sp4.x,sp4.y, tempColor, 1);
}
void setLevel(Level* setLevel) {
p_level = setLevel;
}
};
}
#endif
and
// Level.hpp
#ifndef GAME_LEVEL_HPP
#define GAME_LEVEL_HPP
#include <Moot/Forward.hpp>
#include <Moot/Window.hpp>
#include <Moot/Math.hpp>
#include <GameWindow.hpp>
#include <Camera.hpp>
#include <Corridor.hpp>
#include <Player.hpp>
#include <vector>
namespace Game
{
class Level
{
typedef Corridor* p_corridor;
typedef std::vector<p_corridor> CorridorList;
typedef CorridorList::reverse_iterator ReverseCorridorItter;
CorridorList m_map;
Camera m_camera;
Player m_player;
public:
Level()
{
m_player.setLevel(this);
// Lots of vertices being defined into m_map.
// Loop through and set camera
ReverseCorridorItter rit;
for(rit = m_map.rbegin(); rit != m_map.rend(); rit++)
(*rit)->setLevel(this);
}
~Level()
{
ReverseCorridorItter rit;
for(rit = m_map.rbegin(); rit != m_map.rend(); rit++)
delete (*rit);
m_map.clear();
}
void update()
{
// Temp delete when input and player are implimented.
if(pad[0].buttons & PAD_UP)
m_camera.updateTargetOffsets(0, -2);
if(pad[0].buttons & PAD_DOWN)
m_camera.updateTargetOffsets(0, 2);
if(pad[0].buttons & PAD_LEFT)
m_camera.updateTargetOffsets(-2, 0);
if(pad[0].buttons & PAD_RIGHT)
m_camera.updateTargetOffsets(2, 0);
m_player.update();
ReverseCorridorItter rit;
for (rit = m_map.rbegin(); rit != m_map.rend(); rit++)
(*rit)->update();
}
void draw() // const // EH!!! wtf ReverseIter isn't a member
{
m_player.draw();
ReverseCorridorItter rit;
for (rit = m_map.rbegin(); rit != m_map.rend(); rit++)
(*rit)->draw();
}
Camera& getLevelCamera() {
return m_camera;
}
};
}
#endif
The pointer is being set as far as I can tell, but when I try to access a function from Level, BOOM!
Thanks.
PS: The compiler is gcc 2.95.2 if that makes a difference.
EDIT
Updated with complete code.
You are #include-ing Level's complete declaration:
#include <Level.hpp>
...and then you try to forward-declare Level:
namespace Game
{
class Level;
Don't do this. Choose one or the other. (edit) Or at least put the forward-declaration before the #include-ion of the complete declaration. If all you're doing in game_corridor.hpp is setting pointers to a Level, then a forward declare should do fine. If however you need to call functions on Level from within the HPP file, then you'll need to #include the complete declaration.
EDIT2:
Based on your clarifying edit to your OP, you must #include the complete declaration of Level, and not try to use a forward declaration.
If you forward-declare Game::Level then don't #include it. In a not-so-related note, use #include "header.hpp", not #include <header.hpp>.
Edit as per your updates: Bring the definition of Game::Corridor::update() outside the header and into an implementation file. This way the compile need not know anything about Game::Level apart from the fact that it exists and it's a type.
The problem is that Corridor doesn't know what a Level is, because it can't really #include Level.hpp, because Level.hpp is what #included Corridor.hpp.
The underlying problem is that you're trying to #include a source file. The really underlying problem is that you're using #include when you haven't separated your code into source files and header files. Here's how to split it up. (I'm assuming you're familiar with compiling source files into object files, then linking them into executables.)
Corridor.hpp:
#ifndef GAME_CORRIDOR_HPP
#define GAME_CORRIDOR_HPP
#include <Moot/Math.hpp>
#include <Level.hpp>
namespace Game
{
class Level;
class Corridor
{
static const unsigned int defaultLevelDepth = 800;
Moot::Math::Vector3D wp1, wp2, wp3, wp4;
Moot::Math::Vector2D sp1, sp2, sp3, sp4;
Level * p_level;
public:
Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint);
void update();
void draw();
void setLevel(Level* setLevel);
};
}
#endif
Corridor.cpp:
#include "Corridor.hpp"
namespace Game
{
Corridor::Corridor(Moot::Math::Vector3D setFirstPoint, Moot::Math::Vector3D setSecondPoint)
{
wp1 = setFirstPoint;
wp2 = setSecondPoint;
wp3 = setFirstPoint;
wp3.z += defaultLevelDepth;
wp4 = setSecondPoint;
wp4.z += defaultLevelDepth;
}
void Corridor::update()
{
sp1 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp1);
sp2 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp2);
sp3 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp3);
sp4 = p_level->getLevelCamera().convert3DVectorWithScreenAlgorithm(wp4);
}
// and so on
}