So I am confused. I am getting a redefinition error when trying to implement a method previously declared in a header file. I added include guards to the header and still got the same error. If someone could explain to me what I am not seeing, that would be fantastic.
In file included from main.cpp:2:
./thing.cpp:7:12: error: redefinition of 'method'
int Thing::method(void)
^
./thing.hpp:12:6: note: previous definition is here
int method(void) {};
^
--EDIT--
I am now getting the following:
duplicate symbol __ZN5Thing6methodEv in:
main.o
thing.o ld: 1 duplicate symbol for architecture x86_64
thing.hpp:
#ifndef THING_H
#define THING_H
class Thing {
public:
int a;
int b;
char c;
int method(void) {};
Thing(int anA, int aB, char aC): a(anA), b(aB), c(aC) {};
};
#endif
thing.cpp
#include <iostream>
#include <stdio.h>
#include "thing.hpp"
using namespace std;
int Thing::method(void)
{
return 5;
}
main.cpp
#include <iostream>
#include "thing.cpp"
using namespace std;
Thing* thing = new Thing(5,5,'c');
int main(int argc, char ** argv)
{
cout << thing->method() <<endl;
}
In your header file you have:
int method(void) {};
That's an inline definition, not a declaration. The {} is actually providing the (albeit empty) function body to the compiler. Remove the {} if you want to define the function in another file.
Additionally, you have #include "thing.cpp" rather than #include "thing.hpp" at the top of your main.cpp file.
In main.cpp you need to change:
#include "thing.cpp"
to:
#include "thing.hpp"
Related
My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.
I know there are similar questions but none of these work in my case. Hi I cannot find why I have this issue.
Here is my individual.h file:
#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
class Individual{
private:
vector<unsigned int> chromosome;
unsigned int n_genes;
unsigned int N_colours_used = 0;
unsigned int fitness = 0;
public:
Individual(unsigned int ngenes){};
};
#endif
And this is my individual.cpp file:
#include "individual.h"
Individual :: Individual(unsigned int ngenes){
cout << "something" << endl;
}
The error looks like this
src/individual.cpp:4:1: error: redefinition of ‘Individual::Individual(unsigned int)’
Individual :: Individual(unsigned int ngenes){
^
In file included from src/individual.cpp:1:0:
include/individual.h:24:13: note: ‘Individual::Individual(unsigned int)’ previously defined here
Individual(unsigned int ngenes){};
I tried everything thats in stackoverflow but I still don't know how to solve this problem. Also
"#pragma once" does not work.
Individual(unsigned int ngenes){};
As you can see you have { } after your function declaration, which is a definition of an empty body.
Then you are trying to redefine the body of the function in the .cpp file. Remove { }.
I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp
//This is the header file (header.h)
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
~
~
//This is the cpp file (program.cpp)
#include "header.h"
#include <cstring>
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
I'm getting program.cpp:13: error: 'w' was not declared in this scope
I'm trying to just do the strcpy from data which contain some info to w which is from the private section of the class and using the member function to access them.
I'm not sure if I forgot anything and why I can't access them.
Thanks to the last answer from Sergey Vakulenko
The sequence of the header file is very important.
It should be
#include <cstring>
#include "header.h"
not
#include "header.h"
#include <cstring>
add these headers to your cpp file:
#include <stdio.h>
#include <string.h>
#include "nameofheader.h"
Edit (more full explication ):
for me, that exemple not give any error:
1.h:
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
1.cpp:
#include <stdio.h>
#include <string.h>
#include "1.h"
//This is the cpp file (program.cpp)
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
int main (int argc, char** argv) {
return 0;
}
compled with g++:
g++ 1.cpp -o 1
Your program, the way you are showing it to us here, should compile without problems:
ideone.com/Bj6VU
If you want more help, you should make the all of the two files you are compiling (program.cpp and header.h) available.
just a very small program to test how to use the namespace. I divide it into 3 files, since in large product, ns.h is the namespace interface and ns.cpp is the implementation. I cannot put all these stuff into one file.
Here is the code:
//ns.h
#ifndef MY_H
#define MY_H
namespace my
{
int a=1;
int b=0;
void test();
}
#endif
//ns.cpp
#include <iostream>
#include "ns.h"
using namespace my;
//my::a=1;
//my::b=0;
void my::test()
{
std::cout<<a<<std::endl;
}
//testns.cpp
#include <iostream>
#include "ns.h"
int main()
{
std::cout<<my::b<<std::endl;
my::test();
}
If I keep the above code, and compile will get:
testns.obj : error LNK2005: "int my::b" (?b#my##3HA) already defined in ns.obj
testns.obj : error LNK2005: "int my::a" (?a#my##3HA) already defined in ns.obj
If I comment the statement #include "ns.h" I will get undefined error.
D:\mfc\testns.cpp(5) : error C2653: 'my' : is not a class or namespace name
D:\mfc\testns.cpp(5) : error C2065: 'b' : undeclared identifier
D:\mfc\testns.cpp(6) : error C2653: 'my' : is not a class or namespace name
D:\mfc\testns.cpp(6) : error C2065: 'test' : undeclared identifier
Kindly help me if you know how to do this. Thanks a lot.
Headers are for declarations, not definitions. That's nothing to do with the namespace problem.
//ns.h
#ifndef MY_H
#define MY_H
namespace my
{
extern int a, b; // declared, not defined thanks to 'extern'.
void test();
}
#endif
//ns.cpp
#include <iostream>
#include "ns.h"
int my::a=1; // now we provide the actual definitions.
int my::b=0;
void my::test()
{
std::cout << my::a << std::endl;
}
//testns.cpp
#include <iostream>
#include "ns.h"
int main()
{
std::cout << my::b << std::endl;
my::test();
}
You've defined the two variables a and b in ns.h and then the header file is being included in two source files. This violates the one definition rule as the variables are now defined in both the translation units that are including ns.h.
What you need to do is declare variables in the header and define them in a single source file.
To fix the problem, change ns.h to
#ifndef MY_H
#define MY_H
namespace my
{
extern int a;
extern int b;
void test();
}
#endif
In ns.cpp
#include <iostream>
#include "ns.h"
using namespace my;
int my::a=1;
int my::b=0;
void my::test()
{
std::cout<<a<<std::endl;
}
It's not standard practice to define variables in a header file; they are re-defined every time you #include the header, leading to the linker errors that you are seeing.
If you need to share variables between source files (and there's very few good reasons for this), then you should declare them as extern in the header file, and then define them in one of your source files.