Reputation: 3741
I'm getting
error LNK2005: "bool __cdecl hasLogin(void)" (?hasLogin@@YA_NXZ) already defined in B.obj
error. func
is declared in B.cpp
file, and it's called from A.cpp
file. B.cpp
is included in A.cpp
. Google says that it's not good to include cpp
in antother cpp
. If I copy/paste func
in A.cpp
there's no problem. How to solve tsis problem correctly?
PS I'm new to c++, so forgive if the question is dump.
Upvotes: 1
Views: 3366
Reputation: 1490
write #pragma once in the .cpp//.h where the error is coming, sometimes it avoids the error.
Upvotes: 0
Reputation: 181077
You should generally declare your classes/functions in .h (header) files, and implement them in .cpp files. It's also usually a good idea to put include guards in your .h files so including them multiple times is not a problem. That way your .cpp files can include any .h file without causing any conflicts.
An example; test.h
#ifndef TEST_H
#define TEST_H
class test {
public:
void bop();
}
bool hasLogin();
#endif
and the corresponding .cpp file
#include <iostream>
#include "test.h"
void test::bop() {
std::cout << "bop" << std::endl;
}
bool hasLogin() {
return false;
}
Upvotes: 4
Reputation: 122011
Create a header file named B.h
and add the following function declaration:
#ifndef _B_HEADER_
#define _B_HEADER_
bool hasLogin(void);
#endif
Instead of #include "B.cpp"
in A.cpp
use #include "B.h"
.
Upvotes: 5