when I compile the following c++ code:
#include "ConstantList.h"
using namespace std;
int main() {
ConstantList* cl = new ConstantList();
//do something with cl
delete cl;
cl = NULL;
return 0;
}
The compiler gives me the error:
Undefined symbols:
"ConstantList::~ConstantList()", referenced from:
_main in ccNfeeDU.o
"ConstantList::ConstantList()", referenced from:
_main in ccNfeeDU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Am I not getting the syntax right for instantiating an object?
My ConstantList.h file looks like this:
#ifndef ConstantList_h
#define ConstantList_h
#include <string>
#include "Token.h"
using namespace std;
class ConstantListTail;
class ConstantList {
public:
ConstantList();
~ConstantList();
std::string toString();
void push_back(Token*);
void push_back(ConstantListTail*);
private:
Token* termString;
ConstantListTail* constantListTail;
};
#endif
Any help is greatly appreciated!
Your syntax is correct, because you are getting a linker error, not a compiler error. This error means that you are compiling your main without the source of the ConstantList.cpp, or linking without a reference to ConstantList.o
Compiling with this command should fix the error:
g++ collect2.cpp ConstantList.cpp
(I am assuming that the file with your main function is called collect2.cpp).
"undefined symbol" means you have declared the identifier (in this case the destructor), and it's used, but as far as the linker knows you have not defined it
add a definition somewhere, and make sure the compiled version is in one of the files the linker links
re "syntax for instantiation", unfortunately there is no dedicated syntax for that in C++
instead the functional cast notation is used for constructor invocations
perhaps the closest you get to a pure instantiation syntax is the new expression
re
using namespace std;
in a header file: don't.
for example, the standard library defines something called distance. what are the chances that some code that includes the header will have its own distance, and get a name collision? much higher than zero.
this doesn't mean you should never have using namespace std; in a header file, but you should never have it in the global namespace in a header file. and for other namespaces, be very aware of what that does, namely offering all the standard library names as part of also that namespace.
Related
This question already has answers here:
Separating class code into a header and cpp file
(8 answers)
Closed 5 months ago.
I don't know how to add functions of a class outside its scope to it, use them in another class and then compile it.
MyMain.cpp
#include"MyClass.cpp"
int main(){
MyClass myClass;
myClass.run();
}
MyClass.cpp
#ifndef MYCLASS_CPP
#define MYCLASS_CPP
#include<iostream>
class MyClass {
private:
void usage();
public:
void run();
};
void MyClass::usage(){
std::cout << "usage called" << std::endl;
}
void MyClass::run(){
usage();
}
#endif
I try to compile it with:
g++ MyMain.cpp MyClass.cpp -o main
With that I get the following error message:
/usr/bin/ld: /tmp/ccN7GfOD.o: in function `MyClass::usage()':
MyClass.cpp:(.text+0x0): multiple definition of `MyClass::usage()'; /tmp/ccLhxS6v.o:MyMain.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccN7GfOD.o: in function `MyClass::run()':
MyClass.cpp:(.text+0x38): multiple definition of `MyClass::run()'; /tmp/ccLhxS6v.o:MyMain.cpp:(.text+0x38): first defined here
collect2: error: ld returned 1 exit status
If I have understood the concept correctly, the function headers within the class serve only as placeholders. The actual functionality is then "overwritten" by the external functions, which also contain a body.
And why does the error message say, that the function is already defined in the MyMain.cpp?
I have also seen that there are many similar questions here, but unfortunately I could not expand my understanding of the basic problem to solve it.
Is it possible that I am using the command to build the class with C++ incorrectly or that I can save the #include "MyClass.cpp"?
Kind regards
Several things wrong. here's the steps to put it right
Rename MyClass.cpp to MyClass.h.
Create a new empty file MyClass.cpp
Move the function definitions MyClass::usage() { .. } and MyClass::run() { .. } from MyClass.h to MyClass.cpp. You should probably also move #include <iostream> but this is not essential.
Add #include "MyClass.h" to MyClass.cpp
Change #include "MyClass.cpp" to #include "MyClass.h" in MyMain.cpp
Then build as you are doing now. That part is correct.
Essentially the technique is to separate your code into declarations and definitions. The declarations go into header files, which are included in the cpp files. The cpp files contain the definitions and are what you compile.
I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:
#include <stdio.h>
#include <string>
using namespace std;
class Instrument
{
public:
Instrument();
virtual void print() const = 0;
};
template <class parameter> class Equity : public Instrument
{
public:
Equity();
virtual void print() const;
};
Now, in my main function on main.cpp I only do the following:
#include "instrument.h"
#include <iostream>
int main() {
Equity<double> pb;
return 0;
}
Well, I get the very well-known error:
Undefined symbols for architecture x86_64:
"Equity<double>::Equity()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.
Thanks in advance
You declared the default constructors for both Instrument and Equity but defined them nowhere.
Alter their definitions appropriately:
public:
Equity() = default; // Or {} in pre-C++11
// ^^^^^^^^^
(And equivalently for Instrument)
You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.
I defined namespace "MAPDATA" in a header file(MapData.h) that I have created,and I defined a variable in
"MAPDATA".
MapData.h
namespace MAPDATA{
cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;
}
However, If I write #include "MapData.h" in else class,Linker appear the following error.
duplicate symbol __ZN7MAPDATA11MapSizeDataE in:
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12TileSizeDataE in:
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12MaxTileXDataE in:
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12MaxTileYDataE in:
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
/Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
ld: 4 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But, If I remove name(MAPDATA) of namespace and write #include "MapData.h" in else class, the error disappeared.
After remove name of namespace
namespace {
cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;
}
If someone could explain clearly why the error disappeared,I would be forever grateful.
This is a fairly common error due to the fact that any implementation file that includes that header will contain a definition of those variables. This will lead to duplicate symbols during linking.
It is resolved by defining a single instance of each of those objects in a new, separate, .cpp file. Also it's not clear to me why you are using a namespace with no name.
So first declare those objects with extern:
MapData.h:
extern cocos2d::CCSize MapSizeData;
extern cocos2d::CCSize TileSizeData;
extern int MaxTileXData;
extern int MaxTileYData;
And then add a source file containing the definition of those object:
MapData.cpp:
#include "MapData.h"
#include .... // other files
cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;
Note also, that you might want to put those variables into a class, which can be initialised properly and can maintain all the positive aspects of object orientation.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I recently started working on an interpreter in C++, but I got annoyed that vectors or arrays could not be passed to external class methods no matter what I tried and so I deleted everything I had worked on. As it turns out, I can't pass even an int to another class. I decided to give C++ another chance before resorting to C or Java, but the compiler still doesn't work as I would expect. Maybe I'm forgetting something simple about C++, as I haven't used it in a while, but this seems simple enough. My problem is: I can't pass arguments to methods in other classes when they're not defined in the same file. Here's what I'm trying to do:
Main: main.cpp
#include "myclass.h"
int main() {
MyClass test;
int n = test.add(25, 30);
return n;
}
Header: myclass.h
class MyClass {
public:
int add(int a, int b);
};
Class implementation: myclass.cpp
#include "myclass.h"
int MyClass::add(int a, int b) {
return a + b;
}
Compiling this with g++ main.cpp yields
/tmp/ccAZr6EY.o: In function main':
main.cpp:(.text+0x1a): undefined reference toMyClass::add(int, int)'
collect2: error: ld returned 1 exit status
What the heck am I doing wrong? Also, the compiler yells at me for the same thing even if my functions aren't parameterized, so it must be a problem with the header.
Any help is much appreciated - thanks!
You need to compile both files
g++ main.cpp myclass.cpp
If you only compile main.cpp, the compiler finds the declaration of MyClass::add in your header but the linker later fails to find an implementation of MyClass::add to jump to.
I'm trying to declare an object from a class in another file. I have added the #include "transfer.h" into my metadata.cpp file, but I'm getting the following error:
metadata.o: In function `importMetadata':
metadata.cpp:(.text+0x81): undefined reference to A::B::C::Transfer::Transfer()'
metadata.cpp:(.text+0x81): undefined reference to A::B::C::Transfer::~Transfer()'
And Transfer is defined with in transfer.h
namespace A{
namespace B{
namespace C{
class Transfer {
public:
Transfer();
~Transfer();
int copydata();
... more code goes here.
};
}
}
}
The file transfer.cpp looks like this:
Transfer::Transfer(){
}
Transfer::~Transfer(){
}
I'm also doing an using namespace A::B::C; on the header of the metadata.cpp file. Could someone please help me on that?
In the function int importMetadata() in metadata.cpp I'm declaring Transfer transfer; so in metadata.cpp I'm doing 'transfer.copydata();`
Since you have linker error and not a compiler error this tells you that your #include statement is doing what you want and the compiler recognizes the Transfer class and its constructor. The error occurs when the linker tries to find a reference to the implementation of the Transfer::Transfer() function in one of the .o or .lib files its told to link but cannot find it.
It's likely you have a transfer.cpp to go along with the transfer.h and this file is missing the implementation of the Transfer constructor and destructor. This could be because the functions are missing altogether or have been accidentally defined with a different signature.
So transfer.cpp should look something like:
A::B::C::Transfer::Transfer()
{
}
A::B::C::Transfer::~Transfer()
{
}
Another possibility is that you are trying to link the implementaiton of transfer from a library. In this case you would need to tell your linker to use the .lib file as input. The syntax for this will depend on the compiler you are using.
Hopefully you left out part of your transfer.cpp file, but in case you didnt it should define the namespace to match your header file like such:
namespace A{
namespace B{
namespace C{
Transfer::Transfer(){
}
Transfer::~Transfer(){
}
}
}
}
In case you have fully defined the namespaces, either inline or as above, you'll want to make sure you have the transfer.cpp file included in your project. Seems like the linker is unable to find your source file.