Multiple definition of <List of functions> - c++

I'm sure that the answer is staring me straight in the face, but I haven't been able to make any progress due to this... First some code:
objects/testObject.h:
#include <irrlicht.h>
#include "../maths.h"
using namespace irr;
#ifndef testObject_H
#define testObject_H
class testObject : public scene::SAnimatedMesh
{
public:
testObject(IrrlichtDevice* device);
virtual ~testObject();
protected:
const char* meshInfoLocation;
int totAnims;
private:
};
#endif
objects/testObject.cpp:
#include "testObject.h"
testObject::testObject(IrrlichtDevice* device) : scene::SAnimatedMesh()
{
io::IrrXMLReader* modelInformation = io::createIrrXMLReader(meshInfoLocation);
while(modelInformation->read())
{
if(modelInformation->getNodeName() == "totAnims") totAnims = stringToInt(modelInformation->getAttributeValue("totAnims"));
}
}
testObject::~testObject() { } //Incomplete, but should still compile...
When I compile this code, I get the following errors:
/home/david/workspace/spaceSim/objects/testObject.cpp||In constructor ‘testObject::testObject(irr::IrrlichtDevice*)’:|
/home/david/workspace/spaceSim/objects/testObject.cpp|20|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
/home/david/workspace/spaceSim/main.cpp||In function ‘int main(int, char**)’:|
/home/david/workspace/spaceSim/main.cpp|24|warning: ‘virtual bool irr::io::IFileSystem::addZipFileArchive(const c8*, bool, bool)’ is deprecated (declared at /home/david/irrlicht-1.8.1/include/IFileSystem.h:228) [-Wdeprecated-declarations]|
/home/david/workspace/spaceSim/objects/testObject.cpp||In constructor ‘testObject::testObject(irr::IrrlichtDevice*)’:|
/home/david/workspace/spaceSim/objects/testObject.cpp|20|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
obj/Debug/objects/testObject.o||In function `testObject::testObject(irr::IrrlichtDevice*)':|
/home/david/workspace/spaceSim/objects/testObject.cpp|3|multiple definition of `testObject::testObject(irr::IrrlichtDevice*)'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|3|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::testObject(irr::IrrlichtDevice*)':|
/home/david/workspace/spaceSim/objects/testObject.cpp|3|multiple definition of `testObject::testObject(irr::IrrlichtDevice*)'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|3|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `virtual thunk to testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|29|multiple definition of `virtual thunk to testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|29|first defined here|
obj/Debug/objects/testObject.o||In function `testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|27|multiple definition of `testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|27|first defined here|
obj/Debug/objects/testObject.o||In function `virtual thunk to testObject::~testObject()':|
/home/david/workspace/spaceSim/objects/testObject.cpp|29|multiple definition of `virtual thunk to testObject::~testObject()'|
obj/Debug/main.o:/home/david/workspace/spaceSim/objects/testObject.cpp|29|first defined here|
||=== Build finished: 14 errors, 3 warnings ===|
I've tried the following at solving:
Combining the header and the cpp files.
Emptying out all the method bodies and removing the #includes so that all that matters is the class structure.
Google searches (without any luck...)
Thank you for the help!

I compiled your code using gcc4.8.1 from mingw32 (www.mingw.org) (putting them into file, and replacing missing types). The compilation seems to be OK. I guess the problem could be
#include <irrlicht.h>
#include "../maths.h"
code:
//#include <irrlicht.h>
//#include "../maths.h"
//using namespace irr;
#ifndef testObject_H
#define testObject_H
#include <tuple>
namespace scene {
typedef std::tuple<int,int> SAnimatedMesh;
};
typedef int IrrlichtDevice;
class testObject : public scene::SAnimatedMesh
{
public:
testObject(IrrlichtDevice* device);
virtual ~testObject();
protected:
const char* meshInfoLocation;
int totAnims;
private:
};
#endif
//#include "testObject.h"
testObject::testObject(IrrlichtDevice* device) : scene::SAnimatedMesh()
{
/*
io::IrrXMLReader* modelInformation = io::createIrrXMLReader(meshInfoLocation);
while(modelInformation->read())
{
if(modelInformation->getNodeName() == "totAnims") totAnims = stringToInt(modelInformation->getAttributeValue("totAnims"));
}
*/
}
testObject::~testObject() { } //Incomplete, but should still compile...
int main() {}

Related

invincibility frame and take damage class

i am a beginner and i have already read a doc but i need to practice for learn and now i am stuck.
So i would like to do a class takeDommage for apply a number of dmg and activate a countdown for create the invincibility frame.
so i tryed this (see code under)
It's the first class i create alone so i don't understand what's wrong
main :
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
takeDommage::prendreDegat(10);
std::cout<<pv<<std::endl;
}
takeDommage.h :
#ifndef TAKEDOMMAGE_H
#define TAKEDOMMAGE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
class takeDommage
{
public:
takeDommage();
prendreDegat(int Dommage);
//virtual ~takeDommage();
protected:
sf::Clock takeDammageClock;
int Dommage;
private:
};
#endif // TAKEDOMMAGE_H
takeDommage.cpp
#include "takeDommage.h"
takeDommage::takeDommage()
{
}
void takeDommage::prendreDegat(int Dommage)
{
if(takeDammageClock.getElapsedTime().asSeconds()>=3)
{
std::cout << "bite" << std::endl;
pv -= Dommage;
takeDammageClock.restart();
}
}
error:
||=== Build: Debug in TheGameSFML (compiler: GNU GCC Compiler) ===|
E:\Work\Top_secret\code\TheGame\main.cpp||In function 'int main()':|
E:\Work\Top_secret\code\TheGame\main.cpp|168|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|180|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
E:\Work\Top_secret\code\TheGame\main.cpp|217|error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
E:\Work\Top_secret\code\TheGame\main.cpp|156|warning: unused variable 'enemySpawnTimer' [-Wunused-variable]|
E:\Work\Top_secret\code\TheGame\src\takeDommage.cpp|8|error: prototype for 'void takeDommage::prendreDegat(int)' does not match any in class 'takeDommage'|
include\takeDommage.h|15|error: candidate is: int takeDommage::prendreDegat(int)|
||=== Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
you seem to be very new in c++. the compiler already telling you what's wrong with it.
error: cannot call member function 'int takeDommage::prendreDegat(int)' without object|
you need to instantiate (create) your object first. the way you accessing as if the takeDommage function is a static function which is not. its public a member function of takeDamage
assuming that you have instantiated your takeDommage class somewhere before the if statement call,
TakeDommage Obj;
...
...
if(Collision::PixelPerfectTest(sprite_perso,sprite_ennemis))
{
std::cout<<"collision pp"<<std::endl;
Obj.prendreDegat(10); //calling the prendredegat member function of Obj
std::cout<<pv<<std::endl;
}
in addition to that you are missing the return type void before the function name of prendreDegat
class takeDommage
{
public:
takeDommage();
//prendreDegat(int Dommage); //missing void
void prendreDegat(int Dommage); //correct way. which can be translated as Prendredegat returns nothing.

Multiple definition of error linking my library

i'm trying to create a library with some functions I often use, but compiling I have this error:
Torri_lib.cpp||multiple definition of `inizRandVett(int*, int, int, int)'|
Torri_lib.cpp||first defined here|
Torri_lib.cpp||multiple definition of `printVett(int*, int)'|
Torri_lib.cpp||first defined here|
Torri_lib.cpp||multiple definition of `scambio(int*, int*)'|
Torri_lib.cpp||first defined here|
||=== Build finished: 6 errors, 0 warnings (0 minutes, 0 seconds) ===|
This is the main.cpp:
#include <iostream>
#include "Torri_lib.h"
#define N 10
using namespace std;
void ordina(int*,int);
int main()
{ int vett[N];
srand(time(NULL));
inizRandVett(vett,N,-20,20);
printf("Vettore generato:\n");
printVett(vett,N);
ordina(vett,N);
printf("\n\nVettore ordinato (neg a sx, pos a dx):\n");
printVett(vett,N);
printf("\n\n");
return 0;
}
void ordina(int*vett,int dim)
{ int i,j,neg=0,pos=0;
for(i=0;i<dim;i++)
if(vett[i]<0)
neg++;
else
pos++;
for(i=0,j=neg;i<neg;i++)
if(vett[i]>=0){
while(vett[++j]>=0);
scambio(&vett[i],&vett[j]);
}
}
This is Torri_lib.cpp:
#include <iostream>
#include"Torri_lib.h"
void inizRandVett(int *vett, int dim, int rangeMin, int rangeMax)
{ int i;
for(i=0;i<dim;i++)
vett[i]=rand()%(rangeMax-rangeMin)+rangeMin;
}
void printVett(int *vett, int dim)
{ int i;
for(i=0;i<dim;i++)
printf("%d ",vett[i]);
}
void scambio(int*var1,int*var2)
{
int temp=*var1;
*var1=*var2;
*var2=temp;
}
And this is Torri_lib.h :
void inizRandVett(int*, int, int, int );
void printVett(int *, int);
void scambio(int*,int*);
I don't understand why it says me this error, I don't see multiple definition of the functions.
Can you help me? Thanks!
Maybe you didn't put a guard in your .h file?
Try putting at the beginning of the file:
#ifndef TORRI_H_INCLUDED
#define TORRI_H_INCLUDED
and at the end:
#endif
You need to write in header file to prevent multiple declaration:
#pragma once
// code
OR
#ifndef NAME_H_
# define NAME_H_
// code
#endif

Why does compiler tries to pass a pointer to reference rather than pointer in this code snippet?

I have 5 files:
ExecutionStrategyInterface.h
ExecutorInterface.h
TaskCollectionInterface.h
TaskExecutor.h
TaskExecutor.cpp.
TaskExecutor implements the following member method:
void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
es.execute(tci);
}
At compile time, the compiler calls a member method with a parameter of type pointer to a reference(i.e: mylib::core::TaskCollectionInterface*&).
TaskExecutor.cpp: In member function ‘virtual void mylib::core::TaskExecutor::execute(mylib::core::TaskCollectionInterface*, const mylib::core::ExecutionStrategyInterface&)’:
TaskExecutor.cpp:16: error: no matching function for call to ‘mylib::core::ExecutionStrategyInterface::execute(mylib::core::TaskCollectionInterface*&) const’
./././ExecutionStrategyInterface.h:24: note: candidates are: virtual void mylib::core::ExecutionStrategyInterface::execute(TaskCollectionInterface*) const
make: *** [TaskExecutor.o] Error 1
Can anyone explain me what is happening here please ?
Classes:
ExecutionStrategyInterface.h
#ifndef _EXECUTIONSTRATEGYINTERFACE_H_
#define _EXECUTIONSTRATEGYINTERFACE_H_
class TaskCollectionInterface;
namespace mylib { namespace core {
/**
* Interface for executing a strategy.
*/
class ExecutionStrategyInterface {
public:
/**
* Executes a strategy
*/
virtual void execute(TaskCollectionInterface* tci) const = 0;
};
}} // namespaces
#endif // _EXECUTIONSTRATEGYINTERFACE_H_
TaskCollectionInterface.h
#ifndef _TASKCOLLECTIONINTERFACE_H_
#define _TASKCOLLECTIONINTERFACE_H_
#include "./ExecutionStrategyInterface.h"
namespace mylib { namespace core {
/**
* Interface for a collection of tasks.
*/
class TaskCollectionInterface {
public:
~TaskCollectionInterface();
};
}} // namespaces
#endif // _TASKCOLLECTIONINTERFACE_H_
ExecutorInterface.h
#ifndef _EXECUTORINTERFACE_H_
#define _EXECUTORINTERFACE_H_
class ExecutionStrategyInterface;
class TaskCollectionInterface;
#include "./ExecutionStrategyInterface.h"
#include "./TaskCollectionInterface.h"
namespace mylib { namespace core {
/**
* Interface for an executor.
*/
class ExecutorInterface {
public:
virtual void execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) = 0;
~ExecutorInterface();
};
}} // namespaces
#endif // _EXECUTORINTERFACE_H_
TaskExecutor.h
#ifndef _TASKEXECUTOR_H_
#define _TASKEXECUTOR_H_
#include "./ExecutorInterface.h"
class TaskCollectionInterface;
class ExecutionStrategyInterface;
namespace mylib { namespace core {
/**
* Task Runner.
*/
class TaskExecutor: public ExecutorInterface {
public:
virtual void execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) = 0;
};
}} // namespaces
#endif // _TASKEXECUTOR_H_
TaskExecutor.cpp
#include "./TaskExecutor.h"
#include "./ExecutionStrategyInterface.h"
#include "./TaskCollectionInterface.h"
namespace mylib { namespace core {
void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
es.execute(tci);
}
}} // namespaces
This is confusing because you are forward-declaring the class outside the namespace, so you are ending up with two different classes with the same name. You'll want something like this instead:
namespace mylib {
namespace core {
class TaskCollectionInterface;
class ExecutionStrategyInterface {
.
.
.
};
}
}
The way you have it now, your execute method is taking a pointer to ::TaskCollectionInterface instead of mylib::core::TaskCollectionInterface.
When gcc says type&, it's just its shorthand for saying that you are passing an lvalue so that you know that functions taking a non-const reference are viable candidates.
The problem that you have is that you have declared the method as taking a ::TaskCollectionInterface, but the error message indicates that you are attempting to pass a ::mylib::core::TaskCollectionInterface.
You have a declaration of ::mylib::core::TaskCollectionInterface in TaskCollectionInterface.h that masks the declaration of ::TaskCollectionInterface in the namespace mylib::core.
This is because you are passing a pointer TaskCollectionInterface* tci to the ExecutionStrategyInterface::execute method, while it wants a reference. So you have to dereference that pointer when passing it to that function:
void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
es.execute(*tci);
}

unable to overload virtual function

I have a Network class, which I want two virtual functions which I am going to overload: airtime() and airtime(std::vector<int> freq_bins);
I define the class, and the functions at the bottom:
class Network
{
public:
// Properties of the network protocol
std::string _name;
std::vector<float> _channels;
float _bandwidth;
float _txtime;
// Properties of the specific network
int _id;
macs::mac_t _mac;
protocols::protocol_t _protocol;
bool _static;
float _desired_airtime;
float _act_airtime;
bool _active;
// Constructor
Network();
virtual float airtime() { };
virtual float airtime(std::vector<int> freq_bins) { };
};
Now, I have a second class which I want to overload them. Here is the header of this class:
#ifndef _CSMA_H_
#define _CSMA_H_
#include <string>
#include <vector>
#include <map>
#include "MACs.h"
#include "Protocols.h"
#include "Network.h"
class CSMA : public Network
{
public:
float center_freq;
CSMA();
float airtime();
float airtime(std::vector<int> freq_bins);
};
#endif
I then define them in CSMA.cpp:
#include <iostream>
#include <string>
#include <vector>
#include "MACs.h"
#include "Protocols.h"
#include "Network.h"
#include "Simulator.h"
#include "CSMA.h"
extern Simulator sim;
CSMA::CSMA() {
_mac = macs::CSMA;
}
float CSMA::airtime() {
return _act_airtime;
}
float CSMA::airtime(std::vector<int> freq_bins) {
return 1;
}
I get the warning on returning values, that's not a problem. But the errors I get when trying to compile this, I don't understand:
g++ -o hce_sim hce_sim.cpp Network.cpp CSMA.cpp -Wall
In file included from hce_sim.cpp:2:
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’
Network.h: In member function ‘virtual float Network::airtime()’:
Network.h:53: warning: no return statement in function returning non-void
Network.h: In member function ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’:
Network.h:54: warning: no return statement in function returning non-void
In file included from Network.cpp:6:
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’
Network.h: In member function ‘virtual float Network::airtime()’:
Network.h:53: warning: no return statement in function returning non-void
Network.h: In member function ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’:
Network.h:54: warning: no return statement in function returning non-void
In file included from CSMA.cpp:6:
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’
I created a more simplified program to try and understand why I get this error, yet this simplified program works:
#include <iostream>
#include <vector>
class Base {
public:
Base() { }
virtual void check() { }
virtual void check(bool it) { }
};
class First : public Base {
public:
First() { }
void check() {
std::cout << "You are in check(void) !\n";
}
void check(bool it) {
std::cout << "You are in check(bool) !\n";
}
};
int main() {
First f;
f.check();
f.check(true);
}
Does anyone have any insight here?
Try pure virtual function declarations.
virtual float airtime() = 0;
virtual float airtime(std::vector<int> freq_bins) = 0;
The reason it fails is that you definitions are incorrect, they don't return anything even though you have specified that it should return float.
Tip: You really shouldn't expose the internal state of your class like that. Do some googling on encapsulation.

error: function returning a function

Although there is at least one similar question, I still ask mine since that one hasn't got solved and seems more complicated. I'm trying to simplify mine.
I have a .cpp file that uses .h as below, and compiling these sheds error as follows. Any idea is appreciated. Note that codes are simplified in order to minimally show the problematic parts only.
FC_boost_prove.h:
#ifndef FC_H
#define FC_H
#include <vector>
#include "iostream"
#include "boost/signal.hpp"
#include "boost/bind.hpp"
#include <boost/random.hpp>
typedef boost::signal0<void()> PreUpdateSignal;
typedef PreUpdateSignal::slot_function_type PreUpdateSlot;
typedef boost::signal0<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PostUpdateSlot;
class FC {
public:
FC(uint width, uint height) {
std::cout << "In constructor." << std::endl;
}
~FC() {
//Do ...
}
void connectPreUpdate(PreUpdateSlot s) {
preUpdateSignal_.connect(s);
}
void connectPostUpdate(PostUpdateSlot s) {
postUpdateSignal_.connect(s);
}
protected:
PreUpdateSignal preUpdateSignal_;
PostUpdateSignal postUpdateSignal_;
};
#endif
FC_boost_prove.cpp:
#include <iostream>
#include <string>
#include "FC_boost_prove.h"
int main() {
std::cout << "test." << std::endl;
}
Compile error:
$ g++ FC_boost_prove.cpp
In file included from /usr/include/boost/signals/signal_template.hpp:22,
from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/last_value.hpp: In instantiation of ‘boost::last_value<void()>’:
/usr/include/boost/signals/signal_template.hpp:178: instantiated from ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’
FC_boost_prove.h:12: instantiated from here
/usr/include/boost/last_value.hpp:22: error: function returning a function
In file included from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/signals/signal_template.hpp: In instantiation of ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’:
FC_boost_prove.h:12: instantiated from here
/usr/include/boost/signals/signal_template.hpp:330: error: function returning a function
/usr/include/boost/signals/signal_template.hpp:370: error: function returning a function
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/function0.hpp:11,
from /usr/include/boost/signals/signal_template.hpp:38,
from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::function0<void()>’:
FC_boost_prove.h:24: instantiated from here
/usr/include/boost/function/function_template.hpp:1006: error: function returning a function
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::detail::function::basic_vtable0<void()>’:
/usr/include/boost/function/function_template.hpp:856: instantiated from ‘void boost::function0<R>::clear() [with R = void()]’
/usr/include/boost/function/function_template.hpp:752: instantiated from ‘boost::function0<R>::~function0() [with R = void()]’
/usr/include/boost/signals/slot.hpp:105: instantiated from here
/usr/include/boost/function/function_template.hpp:486: error: function returning a function
/usr/include/boost/function/function_template.hpp:643: error: function returning a function
Environment: Ubuntu 10.10, g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Why are you specifying boost::signal0<>? The signalN templates are for deficient compilers that can't properly parse function signatures.
Either use signal and specify the function signature, as recommended for modern compilers:
typedef boost::signal<void()> PreUpdateSignal;
typedef boost::signal<void()> PostUpdateSignal;
or use signalN and specify the return type (and every argument type) explicitly, as needed for deficient compilers:
typedef boost::signal0<void> PreUpdateSignal;
typedef boost::signal0<void> PostUpdateSignal;