Class is not linked properly [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Sorry if this question maybe asked before but I couldn't find a proper answer for it.
I am trying to use my own class file inside another class which was already written by different people. The whole program is based on Omnet++. Before building everything seems to be alright but as soon as I build the program gives me an error of
undefined reference to `RatePrediction::RatePrediction(double, double, double)
I am trying to use RatePrediction in CentralEntity and this is what I've done:
In RatePrediction.cc :
#include "CentralEntity.h"
RatePrediction::RatePrediction(double gs,double gi, double dataRate):
gs(gs), gi_int(gi), reqDataRate(dataRate), result(0), error(0){
//some codes
}
In CentralEntity.h :
#include "RatePrediction.h"
class CentralEntity : public cAsyncModule
{
//friend classes ....
protected:
virtual void initialize();
}
In CentralEntity.cc:
#include "RatePrediction.h"
void CentralEntity::initialize()
{
RatePrediction RateObj(1.1e-13,1.2-13,1e4);
}
EDIT:
#hildensia Thanks for your comment. I actually did this and now it works. But I am not sure if it can be reliable and stable or not.
I edited the Makefile manually and I added:
OBJS= RatePrediction.o
RatePrediction.o: RatePrediction.cc
$(CXX) -c $(COPTS) RatePrediction.cc

The problem lies within the compilation. RatePrediction.cc seems to be not compiled at all. Thus the linker can't find the Constructor, because there is no object file with it. Your solution in the Makefile seems to be appropriate for now, but will probably be overwritten by your build system. So you should gather information about how you can define which files should be build by Eclipse.

You should define the construction function of Class RatePrediction in RatePrediction.h file.
and it may be better to add such lines "#ifndef *_H #define *_H #endif" in every *.h files.

Related

How do I define an array in a namespace, and access it later in C++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Prevent static initialization order "fiasco", C++
(3 answers)
Closed 6 months ago.
I'm trying to make an array representing a chess board, and I did it like so (according to
the similar solved problems on this site)
board.h
namespace Board {
extern int squares[];
};
board.cpp
#include "board.h"
int Board::squares[64];
Yes, it's suppose to be empty, I'll later fill it with values, but the problem is in my
render function that's drawing the sprites, I have to access them to know what to draw
game.cpp
int piece = Board::squares[index];
That did not throw any error when compiling, but when linking it throws... this
/usr/bin/ld: CMakeFiles/cpp-chess.dir/src/game.cpp.o: warning: relocation against `_ZN5Board7squaresE' in read-only section `.text'
/usr/bin/ld: CMakeFiles/cpp-chess.dir/src/game.cpp.o: in function `Game::RenderPiece()':
game.cpp:(.text+0x9d3): undefined reference to `Board::squares'
What can I do? Thanks a lot in advance!
Thanks a lot to Avi Berger, I realized that I've totally butchered the whole compiling and linking process, oops. It turns out that it all came down to me forgetting to rerun cmake after I added those files, silly me. I appreciate the help a lot, again, thanks everyone!

C++ PlaySound(); returning Undefined reference to PlaySoundW#12 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I've been trying to play a .M4A file with PlaySound(); in code::blocks (C++).
I looked on several websites and found nothing that really helped me. I probably made a REALLY small mistake, but if anybody can help, that would be great.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main()
{
PlaySound("wt.M4A", NULL, SND_ASYNC | SND_FILENAME);
Beep(1000,1000);
getch(); // wait
return 0;
}
I expect the code to play an audio clip, then play a beeping noise, and I have tried removing beep();
instead, I get an error that says :
Undefined reference to PlaySoundW#12
thanks!
You must link against winmm.lib / libwinmm.a in order to have PlaySoundA/PlaySoundW functions.
Also, be careful with unicode: you are calling PlaySoundW but you pass an ANSI string.
Make sure to call PlaySoundA or use unicode strings.
To use unicode strings, there is the TEXT macro and you must #define UNICODE or -DUNICODE command-line option.

c++ LNK2001 & LNK1120 compile errors [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am debugging some C++ software, and want to modify an existing function slightly, so that it changes the value of a particular variable used elsewhere in the program.
The function is currently defined as so:
void DataStore::setEraseSelected(){
...
// Function code here
...
}
As it stands, the function works correctly with no problems whatsoever. But, I now want to modify the function, so that it will change the value of a variable used elsewhere in the program. To do this, I have tried passing the variable into the function as a parameter (I have also updated the header file to reflect the changes to the function), and then assigning a value to the variable inside the function:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg){
...
// Function code here
...
statusMsg.CODE_ERASE = Types::Activated;
...
}
As mentioned, I have added the declaration to the header file, so that it now has the declaration for the function with the parameter, as well as the one for the function without the parameter:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg);
But when I try and build the code (using Visual Studio 2010), I get the following two compile errors:
error LNK2001: unresolved external symbol "public void_thiscall DataStore::setEraseSelected(void)" (? setEraseSelected#DataStore::QAEXXZ)
error LNK1120: 1 unresolved externals
The first error highlights the project .obj file, which I have tried deleting and building again, but get the same error, and second one highlights the project .exe file, which I have also tried deleting and building again, but get the same error.
Anyone have any ideas why? I've had a look on SO for questions regarding these errors, but none of them seem to clearly explain why I might be getting them. They all seem to suggest that the compiler is possibly looking in the wrong place, but if I undo my changes, then the code compiles with no problems, and I haven't told the compiler to look anywhere else when building the code with my changes...
void DataStore::setEraseSelected(int );
Pass that variable

Call a system C++ function from C [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I have a C library, which I'm not really allowed to modify in substantial way, that heavily uses the random function, which is not available on my Visual Studio 2010 compiler (see another one of my questions)
So, I have to write my own random method. I tried to use a simple random() { srand(time(NULL)); return rand(); } but the library main conceptor doesn't like it (he's on Mac and I think he doesn't care much about my problems). He tells me to run srand() only once when the library is run, but since there might be multiple entry points in this lib, I don't know how to do it. If you have a solution to this problem, I'm all ears.
Update: I have found out that the "main" function is always run during the loading of the library (from what I understand), so I'll use this for the seeding. But I'm still interested in the solution to this particular question.
Update 2: well, calling srand() from main() produces always the same results, so that's not the answer after all :(
So I decided to follow another method of generating random numbers:
#include <Windows.h>
#include <wincrypt.h>
long int random() {
HCRYPTPROV prov;
if (CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
long int li = 0;
if (CryptGenRandom(prov, sizeof(li), (BYTE *)&li)) {
return li;
} else {
// random number not generated
return 0;
}
if (!CryptReleaseContext(prov, 0)) {
// context not released
return 0;
}
} else {
// context not created
return 0;
}
}
But then I get this kind of error:
error LNK2019: unresolved external symbol __imp__CryptReleaseContext#8
referenced in the function _random
The problem, if I understand correctly, is that I'm calling C++ functions from C, and C doesn't understand the name mangling happening during C++ compilation.
I have visited the classic answer to calling C++ from C, but I didn't see a way to call a system C++ function from a C method. Is there something I've missed?
Thanks!
Your problem seems to be something else: you have to link against Advapi32.lib to use the function CryptReleaseContext.
To do so:
Right click your project -> Properties -> Linker -> Input -> Additional Dependencies
Make sure, that Advapi32.lib is in the semicolon seperated list - otherwise add it there and rebuild your project. Then resolving the symbol __imp__CryptReleaseContext should be possible for your linker.

undefined reference to `vtable for myClass' [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined reference to vtable. Trying to compile a Qt project
here is the code
#include <iostream>
#include <QApplication>
#include <QTimer>
class myClass : public QObject {
Q_OBJECT
public:
QTimer *timer;
myClass(){
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(mySlot()));
timer->start(1000);
}
public slots:
void mySlot() {
std::cout << "Fire" << std::endl;
}
};
int main() {
std::cout << "Hello, world";
myClass atimer;
return 0;
}
Apart from the error, there are two more things I don't understand:
Why there isn't any semicolon after macros, in this case Q_OBJECT. It doesn't seem to obey C++ syntax but yet people write code like that.
The "public slots" is a modifier created by Qt, but how come gcc compiler can still understand it. How could an IDE like Qt modify the standard syntax of a language?
You didn't give the exact error message, but I suspect what's happening is that you didn't run moc on your code, or you didn't compile the code generated by moc, or you didn't link the code into your executable/library.
As for your other questions:
You don't need to have a semicolon after macros; the preprocessor doesn't care about semicolons - only the compiler does. So whether or not you need to add a semicolon manually depends on what your macro (Q_OBJECT) in this case expands to, and where you use it. In your case, no semicolon is needed.
slots is an macro which expands to an emtpy string, so any C++ compile can process it. However, slots is also recognized as a special key word by moc. The same goes for signals, by the way (it's a macro expanding to protected:).
This is just because you didn't run qmake since you aded Q_OBJECT. Just run qmake (if you use QtCreator, it must be in the Build Menu) and then compile ;).
Hope it helped
Usually an undefined reference to vtable indicates that you declared some virtual functions, but never provide the definition to them. Perhaps Q_OBJECT is declaring something?
Macros are expanded before C++ syntax is considered, working in textual form. That is why macros themselves do not have to obey C++ syntax. If, for example, Q_OBJECT contains a semicolon at the end of its definition, so that after the substitution you get a correct C++ code, then that is good enough.
slots may be a macro as well (maybe even an empty one). Then, after substituting slots with nothingness you get a valid C++ code again.
You have to use the Meta Object Compiler delivered by QT
In general if you're getting an undefined reference to vtable error it's because qmake hasn't ran and generated a necessary moc for it. Rerunning qmake in the project directory should fix it, if it doesn't then clean the build and run quake and make again.