Using a class/struct/union over multiple cpp files C++
I am trying to create a class in C++ and be able to access elements of that class in more than one C++ file. I have tried over 7 possible senarios to resolve the error but have been unsuccessful. I have looked into class forward decl开发者_StackOverflow社区aration which doesen't seem to be the answer (I could be wrong).
//resources.h
class Jam{
public:
int age;
}jam;
//functions.cpp
#include "resources.h"
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
int main(){
printme();
std::cout << jam.age;
}
Error 1 error LNK2005: "class Jam jam" (?jam@@3VJam@@A) already defined in stdafx.obj
Error 2 error LNK1169: one or more multiply defined symbols found
I understand the error is a multiple definiton because I am including resources.h
in both CPP files. How can I fix this? I have tried declaring the class Jam
in a CPP file and then declaring extern class Jam jam;
for each CPP file that needed to access the class. I have also tried declaring pointers to the class, but I have been unsuccessful. Thank you!
You're declaring the structure Jam
and creating a variable called jam
of this type. The linker is complaining that you've got two (or more) things called jam
, because your header causes each .cpp
file to declare one of its own.
To fix this, change your header declaration to:
class Jam{
public:
int age;
};
extern Jam jam;
And then place the following line in one of your .cpp
sources:
Jam jam;
You need to separate your definition from your declaration. Use:
//resources.h
class Jam{
public:
int age;
};
// Declare but don't define jam
extern Jam jam;
//resources.cpp
// Define jam here; linker will link references to this definition:
Jam jam;
The variable jam
is defined in the H file, and included in multiple CPP classes, which is a problem.
Variables shouldn't be declared in H files, in order to avoid precisely that. Leave the class definition in the H file, but define the variable in one of the CPP files (and if you need to access it globally - define it as extern
in all the rest).
For example:
//resources.h
class Jam{
public:
int age;
};
extern Jam jam; // all the files that include this header will know about it
//functions.cpp
#include "resources.h"
Jam jam; // the linker will link all the references to jam to this one
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
int main(){
printme();
std::cout << jam.age;
}
As a first step you should separate the definition of the class and declaration of an instance. Then use extern in resources.h and declare the instance in a CPP.
Like this:
//resources.h
class Jam{
public:
int age;
};
extern Jam jam;
//functions.cpp
#include "resources.h"
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
Jam jam;
int main(){
printme();
std::cout << jam.age;
}
Use the pragma once directive, or some defines to make sure a header doesn't get included more than once in your code.
Example using pragma directive (example.h):
#pragma once
// Everything below the pragma once directive will only be parsed once
class Jam { ... } jam;
Example using defines (example.h):
#ifndef _EXAMPLE_H
#define _EXAMPLE_H
// This define makes sure this code only gets parsed once
class Jam { ... } jam;
#endif
精彩评论