Ok, this has been covered already, for example here: static array class variable "multiple definition" C++
But I am missing some details here.
I have got the following classes:
Foo.cpp
#include <iostream>
#include "Model.h"
int main(int argc, char** argv){
std::cout << "hello" << std::endl;
return 0;
}
Model.h
#ifndef MODEL_H
#define MODEL_H
#include <string>
#include "md2Loader.h"
class Model{
public:
Model();
Model(const std::string& model_file);
private:
md2_header_t header;
modelData_t model;
};
#endif
Model.cpp
#include "Model.h"
#include "md2Loader.h"
Model::Model(){}
Model::Model(const std::string& model_file){
model = md2Loader::load_model(model_file);
}
and md2Loader.h
#ifndef MD2LOADER_H
#define MD2LOADER_H
struct modelData_t{
int numVertices;
int numTextures;
// etc
};
struct md2_header_t {
std::string version;
};
class md2Loader{
public:
md2Loader(){};
static modelData_t load_model(const std::string& model_file);
};
modelData_t md2Loader::load_model(const std::string& model_file){
modelData_t result;
result.numVertices = 1000;
result.numTextures = 10;
return result;
}
#endif
The linker complains of multiple definitions. But I am not quite sure, why. Do the #ifndef, #define preprocessor directives not help? I kind of get it that md2Loader.h get included to both Model.cpp and Model.h. When I do the implementation in Model.h and leave Model.cpp away it will compile and link just fine. I thought, that the directives for the preprocessor prevent that from happening, but obviously I am missing something there.
You need to either inline the function defined in header file or move it into a source file. Because that function will be appeared in multiple translation unit.
inline modelData_t md2Loader::load_model(const std::string& model_file){
modelData_t result;
result.numVertices = 1000;
result.numTextures = 10;
return result;
}
See more on inline specifier
There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit. For example, an inline function or an inline variable (since C++17) may be defined in a header file that is #include'd in multiple source files.
Related
I have a C++ application with the following 3 files:
// sample.h
#ifndef sample_h
#define sample_h
#include <stdio.h>
namespace mynamespace {
class sample {
public:
void myprintf(const char* tmp);
};
}
#endif
// sample.cpp
#include "sample.h"
void mynamespace::sample::myprintf(const char* val) {
printf(val);
}
// main.cpp
#include "sample.h"
int main() {
mynamespace::sample sample1; // How to omit this line?
sample1.myprintf("Hello world!");
}
Is it possible to remove the instantiation of sample1 object from main.cpp, and to have it already available (coming from the static library "sample.obj")?
If I move that line to sample.h, then the error I get during compilation is:
"class mynamespace::sample sample1" already defined in sample.obj
If I move that line to sample.cpp, then the error message is:
'sample1': undeclared identifier
Actually I understand why both errors occur, I just don't know what is the solution.
Thanks
Use static declaration:
in sample.h
namespace mynamespace{
class sample {
public:
static sample sample1;
void myprintf(const char* tmp);
};
static sample& sample1 = sample::sample1;
}
then in sample.cpp
mynamespace::sample mynamespace::sample::sample1;
from main.cpp
access the variable
mynamespace::sample::sample1.myprintf("");
mynamespace::sample1.myprintf("");
Specify an extern storage class in the header file:
namespace mynamespace {
// ...
extern sample sample1;
}
And then define it normally in sample.cpp:
mynamespace::sample sample1;
It's possible that on some compilers/operating systems it will be necessary to specify something else, something that's compiler-specific, in order to get external linkage to a data symbol in a library. This is beyond the scope of the C++ standard. Consult your compiler's documentation for more information.
I've got two files, list.cpp and Header.h. Segments of the files are below. I know that if the header file is for a class, it is setup different. E.g.
class MyClass
{
public:
void foo();
int bar;
};
However, since I'm not really working with a class here (correct me if I'm wrong), am I not able to declare things under public: and private like below?
Also, if I were to place the global variable rescan in the header file as a member variable, below the function definitions, only the main function can see the variable. Why is it not within the scope of the other functions?
list.cpp:
#include <boost/algorithm/string.hpp>
#include <vector>
using namespace std;
vector<int> results;
bool rescan;
int main()
{
vector<vector<string>> list;
int success = readFile(list);
vector<vector<string>> bad = findMe(list);
system("pause");
return 0;
}
vector<vector<string>> findMe(vector<vector<string>> find)
{
rescan = true;
}
Header.h:
#pragma once
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <string>
#include <vector>
std::vector<std::vector<std::string>> findMe(std::vector<std::vector<std::string>>);
#endif
EDIT: I tried this in my header file:
public:
bool rescan;
But I got "syntax error: 'public'
If you want your global to be visible in other translation units (TU) (other files), you have to declare them extern in those other TUs:
Header.h:
// Include guard omitted
extern bool rescan; // Declaration
file.cpp
#include "Header.h"
bool rescan = false; // Definition
// ...
file2.cpp
#include "Header.h" // To see extern bool rescan;
void foo()
{
rescan = true;
}
// ...
Consider this code.
//header.h
int x;
//otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler erred with the message. "fatal error LNK1169: one or more multiply defined symbols found"
but when I add static before x, it compiles without errors.
And here is the second case.
//header.h
class A
{
public:
void f(){}
static int a;
};
int A::a = 0;
/otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler again erred with multiple declaration.
Can anybody explain me the behavior we static variables in classes and in global declarations?? Thanks in advance.
The issue with the static member variable is that you have the definition occur in the header file. If you #include the file in multiple source files, you have multiple definitions of the static member variable.
To fix this, the header file should consist only of this:
#ifndef HEADER_H
#define HEADER_H
// In the header file
class A
{
public:
void f(){}
static int a;
};
#endif
The definition of the static variable a should be in one and only one module. The obvious place for this is in your main.cpp.
#include "header.h"
int A::a = 0; // defined here
int main()
{
}
Declare x as extern in header.h to tell the compiler that x will be defined somewhere else:
extern int x;
Then define x once in the source file which you think is most fitting.
For example in otherSource.cpp:
int x = some_initial_value;
How to fix error in Hero.h ?
GCC C++ compiler flags : -c -fmessage-length=0 -std=gnu++11 ;
I update g++ to 4.8.1
// Creature.h
#pragma once
#ifndef CREATURE_H_
#define CREATURE_H_
#include <string>
#include "Hero.h"
#include "Characteristics.h"
#include <map>
class Creature
{
private:
CreatureCharacteristics Characters;
Creature(const std::string i_name, int i_count = 0);
Creature(const Creature& Donor);
public:
typedef std::map < std::string, Creature* > Prototypes;
static Prototypes Clones_Bank;
~Creature();
const CreatureCharacteristics& Get_characteristics(){
return this->Characters;
}
static Creature*& Clone(std::string i_name, int i_count = 0);
};
#endif /* CREATURE_H_ */
// Hero.h
#pragma once
#ifndef HERO_H_
#define HERO_H_
#include "Creature.h"
#include "Characteristics.h"
#include <string>
#include <vector>
typedef std::vector<Creature*> Army; // ERROR HERE (‘Creature’ was not declared in this
scope)
class Hero {
private:
Army army;
HeroCharacteristics base_characteristics;
public:
Hero(std::string name = '\0', int attack = 0, int defense = 0):
hero_name(name)
{
base_characteristics.attack = attack;
base_characteristics.defence = defense;
};
const Army& Get_army() const
{
return army;
};
const std::string& Get_name() const
{
return hero_name;
};
const HeroCharacteristics& Get_characteristics() const
{
return base_characteristics;
};
void Add_creature(Creature* creature, int creature_count);
};
#endif /* HERO_H_ */
The problem is that Hero.h and Creature.h include each other: you have a cyclic dependency. When Hero.h includes Creature.h and Creature.h tries to include Hero.h again, HERO_H_ is already defined, and thus nothing gets inserted (if you removed the include guards, you would get an endless include cycle which is no good either).
However, it seems that Creature.h does not actually use Hero.h, so you can just remove this header. If you later do need something from the header, you may very well get away with a forward declaration. For more on this, see the C++ FAQ entry "How can I create two classes that both know about each other?".
I got three .cpp files and two header files.
But when i compile them, meaning the Point.cpp, Data.cpp and main.cpp, it will say
Data.h:6:7 redefinition of Data at 'Data.h'
Data.h:6:7 previously definition of 'class Data'
Below is my Data.h(previously known as 2.h at above)
#include <iostream>
#include <string>
using namespace std;
class Data
{
private:
string sType;
public:
Data();
Data(string);
void setSType(string);
string getSType();
};
Below is my data.cpp
#include "Data.h"
Data::Data()
{
sType = "";
}
Data::Data(string s)
{
sType = s;
}
void Data::setSType(string ss)
{
sType = ss;
}
string Data::getSType()
{
return sType;
}
Below is my PointD.h (previously known as 3.h)
#include <iostream>
#include <string>
#include "Data.h"
using namespace std;
class PointD
{
private:
int x
Data data1;
public:
PointD();
PointD(int,Data);
void setX(int);
void setData(Data);
int getX();
Data getData();
};
Below is my PointD.cpp
#include "PointD.h"
PointD::PointD()
{
x = 0;
}
PointD::PointD(int xOrdinate,Data dd)
{
x = xOrdinate;
data1 = dd;
}
void PointD::setXordinate(int Xordinate)
{
x = Xordinate;
}
void PointD::setData(Data dd)
{
data1 = dd;
};
int PointD::getXordinate()
{
return x;
}
Data PointD::getData()
{
return data1;
}
This is my main.cpp
#include <iostream>
#include <string>
#include "Data.h"
#include "PointD.h"
using namespace std;
int main()
{
const int MAX_NUM = 20;
Data ldata[MAX_NUM];
PointD pointd[MAX_NUM];
//more codes..
}
But when i compile them, meaning the Point.cpp, Data.cpp and main.cpp, it will say
Data.h:6:7 redefinition of Data at 'Data.h'
Data.h:6:7 previously definition of 'class Data'
Can anybody let me know whats actually went wrong here..
You need to use include guards, or the easiest:
#pragma once
in your header files
See Purpose of Header guards for more background
Idea: 1.hpp
#ifndef HEADER_GUARD_H1_HPP__
#define HEADER_GUARD_H1_HPP__
// proceed to declare ClassOne
#endif // HEADER_GUARD_H1_HPP__
In each of your header files write:
#ifndef MYHEADERNAME_H
#define MYHEADERNAME_H
code goes here....
#endif
Its better like this:
#ifndef DATA_H /* Added */
#define DATA_H /* Added */
#include <iostream>
#include <string>
// using namespace std; /* Removed */
class Data
{
private:
std::string sType;
public:
Data();
Data( std::string const& ); // Prevent copy of string object.
void setSType( std::string& ); // Prevent copy of string object.
std::string const& getSType() const; // prevent copy on return
std::string& getSType(); // prevent copy on return
};
#endif /* DATA_H */
The big fix is adding ifndef,define,endif. The #include directive works as if copying and pasting the .h to that line. In your case the include from main.cpp are:
main.cpp
-> Data.h (1)
-> Point.h
-> Data.h (2)
At (2), Data.h has already been `pasted' into main.cpp at (1). The class declaration of Data, i.e. "class Data{ .... };" , appears twice. This is an error.
Adding include guards to the top and bottom of every .h are standard practice to avoid this problem. Don't think about it. Just do it.
Another change I'd suggest is to remove any "using namespace ..." lines from any .h . This breaks the purpose of namespaces, which is to place names into separate groups so that they are not ambiguous in cases where someone else wants an object or function with the same name. This is not an error in your program, but is an error waiting to happen.
For example, if we have:
xstring.h:
namespace xnames
{
class string
{
...
};
}
Foo.h
#include <xstring>
using namespace xnames;
...
test.cxx:
#include "Foo.h"
#include "Data.h" // Breaks at: Data( string ); -- std::string or xnames::string?
...
void test()
{
string x; // Breaks. // std::string or xnames::string?
}
Here the compiler no longer knows whether you mean xnames::string or std::string. This fails in test.cxx, which is fixable by being more specific:
void test()
{
std::string x;
}
However, this compilation still now breaks in Data.h. Therefore, if you provide that header file to someone, there will be cases when it is incompatible with their code and only fixable by changing your header files and removing the "using namespace ...;" lines.
Again, this is just good coding style. Don't think about it. Just do it.
Also, in my version of Data.h, I've changed the method parameters and return types to be references (with the &). This prevents the object and all of its state from being copied. Some clever-clogs will point our that the string class's is implementation prevents this by being copy-on-write. Maybe so, but in general, use references when passing or returning objects. It just better coding style. Get in the habit of doing it.