This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 9 years ago.
I'm trying to migrate some legacy code into a newer project and I don't really get this one fixed. The code compiled and worked well in the older environment.
I have a header file which contains these definitions:
std::string ToString(shared_ptr<const SomeObject> obj);
std::string ToString(SomeObject* obj);
And an implementation file with following lines:
using namespace std;
string ToString(shared_ptr<const SomeObject> obj)
{
// code cut
return outstring.str();
}
string ToString(SomeObject* obj)
{
// code cut
return outstring.str();
}
I'm trying to compile it with clang and I get the following redefinition error:
.../Filename.cxx:15:8: error: redefinition of 'ToString' as different
kind of symbol
string ToString(shared_ptr<const SomeObject> obj)
^
.../Filename.h:15:13: note: previous definition is here
std::string ToString(SomeObject* obj);
Why is it a redefinition as different kind of symbol? How should I fix this? And last but not least, why does it work with older compilers?
Check if string and shared_ptr are declared, and try specifying namespaces for them (replace shared_ptr with boost::shared_ptr or std::shared_ptr) to make sure that the same class is used in declaration and implementation of ToString.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I have the following part of code in testtournamentmember.cpp
TournamentMember player1("Ian", "Smith", "1998-12-03");
TournamentMember player2 = TournamentMember(player1);
player1.setFirstName("Andrew");
with class TournamentMember.h defined as:
private:
char firstName[31];
char lastName[31];
char dateBirth[11];
static std::string location;
//static int numberMembers;
//static int difficulty;
public:
TournamentMember();
TournamentMember(char[], char[], char[]);
TournamentMember(const TournamentMember&);
~TournamentMember();
inline void setFirstName(char*);
and with TournamentMember.cpp with:
inline void TournamentMember::setFirstName(char* _firstName){
strcpy(firstName, _firstName);
}
(I have all the other functions defined, but I didn't attached them). When I want to run the code, I receive undefined reference to 'TournamentMember::setFirtstName(char*). I do not understand what is wrong with my code, because I define the fuction setFirstName as char* in the class and also in the program.
Your definition of setFirstName in TournamentMember.cpp is marked inline. That means the definition exists only in TournamentMember.cpp; that definition is not accessible from other source files, such as testtournamentmember.cpp.
By the way, you should be getting this error when you go to compile the code, not run it. You might be using a process that compiles and runs in one step, but you should still be aware of the distinction.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've searched many answers but none of them can solve my problem, I'm new to c++, this issue is quite wired to me. Below is a simplified extraction of my code.
TestHeader.h:
#ifndef NAMESPACE_TESTHEADER_H_
#define NAMESPACE_TESTHEADER__H_
namespace Namespace {
class TestHeader {
public:
TestHeader(const std::string& str) : anyString_(str) { }
virtual std::string methodOne(const std::string& param) const;
virtual ~TestHeader() { anyString_.clear(); }
protected:
std::string anyString_;
};
}
#endif //NAMESPACE_TESTHEADER__H_
TestHeader.cpp:
#include "TestHeader.h"
using namespace std;
namespace Namespace {
TestHeader::TestHeader(const std::string& str):anyString_(str) { <do something>; }
std::string TestHeader::methodOne(const std::string& param) const
{
return <A string>;
}
TestHeader::~TestHeader() {
anyString_.clear();
}
}
What I did was simply call this line in any other .cpp in my package:
#include "TestHeader.h"
TestHeader testHeader("whatever");
The build failed by throwing
error: undefined reference to 'vtable for Namespace::TestHeader'
the vtable symbol may be undefined because the class is missing its key function
The most weird thing is: if I comment out virtual std::string methodOne(const std::string& str) const; in header and its implementation in .cpp, OR, if I comment out : anyString_(str) after constructor and anyString_.clear(); in destructor together in header only, the build will succeed.
Firstly You should not define the constructor and destructor twice. It shouldn't be compiling as mentioned by Curious in comments
Second I assume that you want don't the class to be abstract as there is no Runtime polymorphism implemented which is the basic use of Virtual functions.
If you don't want the class TestHeader to be abstract remove the virtual keyword which is referring to Virtual Table.C++ compiler inserts Virtual Table for every class having virtual function or class inherited from the class that has virtual functions.
Better study the use of Virtual keyword and then write the code.
Here are quick links for the same
Link 1
Link 2
Also, I think you need to revisit few concepts from Destructor virtual ~TestHeader() { anyString_.clear(); } does not make any sense. In fact, there is no base class which in turn denies the use of Virtual Destructor which is used in case of Inheritance
Firstly, include #include <string> at the top of your header file. I am guessing the error is because you have not linked the object file produced after compiling TestHeader.cpp with the source file that contains the declaration and initialization for the variable named testHeader
Compile these with the following command and you should see a linker error that complains saying that you have multiple definitions for the constructor
g++ -std=c++14 TestHeader.cpp yourfile.cpp
After you see those errors, remove the multiple definitions, either put all your definitions in the cpp file or only put them in one place and then recompile and link with the above command. The linker error should be gone.
This question already has answers here:
error: unknown type name ‘bool’
(4 answers)
Closed 6 years ago.
I want to understand this error: syntax error before 'bool', on the following code:
typedef struct hdate{
date_arc_u date;
unsigned short time;
bool test;
}PACKED_ST horodate_a
When I change bool to another type there is no error.
I already use bool in others parts of the code without error.
I don't understand this error here ....
It's probably because you are writing C code, and the bool type does not exist in C. Your file extension is probably .c, not .cpp, and your code definitely looks like it was written in C.
Probably your C compiler doesn´t know about bool type.
You can try:
1- Including this at first #include <stdbool.h>
2- Declaring at first typedef enum bool { false, true };
This question already has answers here:
Why doesn't ADL find function templates?
(4 answers)
Closed 9 years ago.
Could someone please explain why the following code is giving error (error C2065: 'select' : undeclared identifier) at compile time:
namespace N {
class MyClass{
};
template<int I> void select(MyClass*)
{}
}
void g (N::MyClass* mp)
{
select<10>(mp);
}
void main()
{}
According to Argument Dependent Lookup, this should work fine, since I have specified N:: in `g``s argument. So, select should be visible to compiler.
Why does ADL not work here?
have you tried N::select?
either that or a
using namespace N
should work since simply select is not visible
Any time you utilise a class from a divergent namespace from the one you are currently in you must either reference it directly (N::select) or set up a using namespace (using namespace N;) or set up a direct using statement to it for future use (using N::select)
For disambiguation I would look at this and this , which between them should give you a good start on how/why you cannot simply call select.
Cheers, and feel free to get a hold of me for more info.
This question already has answers here:
C variable declarations after function heading in definition [duplicate]
(3 answers)
What weird C syntax is this? [duplicate]
(3 answers)
Closed 9 years ago.
I've been given a C/C++ code that looks like this:
extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi, extrafield_local)
zipFile file;
const char* filename;
const zip_fileinfo* zipfi;
const void* extrafield_local;
{
... function body
}
Is declaring the parameters of a function like that possible? I'm getting errors from the compiler (g++).
Thanks in advance.
This is a very old-school C (pre-ANSI C syntax) way for doing things. I suggest you change it, if you own the code, to
extern int ZEXPORT zipOpenNewFileInZip3 (
zipFile file,
const char* filename,
const zip_fileinfo* zipfi,
const void* extrafield_local)
...
There are some more details here and here
That is ancient syntax for defining functions in C. It predates the first standardized version of the C language. More importantly, that syntax has never been valid C++. Since you are compiling this code (which is obviously C code) with a C++ compiler, it is failing.