-
Undefined Reference to..
Hi,
I'm trying to compile a simple program using CodeBlocks 8.02, mainly 3 files: main.cpp, matriz.h and matriz.cpp. However, I keep getting an "undefined reference error". I ve been looking for a solution on Internet and any of them fixes the problem. So far, I tried this:
- Using WinMain instead of main().
- Main with parameters.
- Using END_OF_MAIN().
and many more....
Please HELP!!!
Thanks.
-
Undefined reference means that your linker couldn't find a particular function. Link in the object file or library containing that function.
Can't tell you much more. What was the error?
-
Trying random things to fix an error is not the way to handle an error.
Next time give the full error and an idea of what you're actually doing. What you told is not that helpful other than to tell you that it's a linker error.
-
Thanks for answering. Ok, the error appears for some strange reason, everything seems to be working fine I change some lines and then the error appears, so I change it back to its original state and the error does not disappear.
It says undefined reference to every function declared on matriz.h.
-
Sounds like you're not linking both main.cpp and matriz.cpp.
-
I pretty sure it is a CodeBlocks problem, any idea of how to fix it?
-
Exactly what I previously said. Check to make sure your project is compiling and linking both .cpp files.
This reminds me of an original Knight Rider episode where Michael asks Bonnie if KITT is all ready after she was working on him. Bonnie replies that KITT is OK except for one factor: the nut behind the wheel. :rolleyes:
Case in point, don't blame your tools right away.
-
Haha, I' ve been looking for a solution for a while now, that 's why I'm looking for help :).
-
1. Make sure that all of the files that you have are included in the project, both debug and release.
2. If you declare a function in a header file as inline, but have defined it in a .cpp file, move it into the header file.
3. Make sure your function arguments match everywhere.
4. Delete any object files that Code::Blocks has been using and "rebuild."
These have been my most common problems.
-
I ve tried all of them..unfortunately the problem is not fixed.
-
With such vivid descriptions of your problem, it must be our fault. :rolleyes:
If you want help with the exact details of your IDE, you have to give us more to work on about what you are doing or not doing.
This is, btw, why I think newbies should not be allowed to touch an IDE until they have a better idea of what it actually does. Getting lost in how to communicate to the linker.... Bleh.
-
Ok.
main.cpp
Code:
#include <iostream>
#include "Matriz.h"
using namespace std;
int main(int argc, char *argv[])
{
Matriz a;
a.Imprimir();
return 0;
}
matriz.h
Code:
#ifndef MATRIZ_H
#define MATRIZ_H
#include <iostream>
using namespace std;
class Matriz
{
private:
int ** matriz;
int filas;
int columnas;
public:
Matriz();
void Imprimir();
};
#endif // MATRIZ_H
matriz.cpp
Code:
#include "Matriz.h"
Matriz::Matriz()
{
filas=4;
columnas=4;
matriz = new int * [filas];
for(int i=0; i<filas; i++){
matriz [i] = new int [columnas];
for(int j=0; j<columnas; j++)
matriz[i][j]=0;
}
}
void Matriz::Imprimir(){
for(int i=0 ; i<filas ; i++){
for(int j=0 ; j<columnas ; j++)
printf("%5d",matriz[i][j]);
cout<<endl;
}
cout<<"------------------------------"<<endl;
}
-
That is not a description of the errors you are getting. Nor, for that matter, is it a complete description of your setup. Code::Blocks will let you open as many files as you want. It won't put them in a project for you unless you specify such. Have you done so? Are you trying to build a single file or build your project?
-
It is a project, containing those 3 files. It is a project and then I added a class, and then I used include statement in order to use it.