undefined reference to `ReadFromFile(int&, int&, int**&)'|

Hello,

I get this message from Codeblocks when it tries to link my project files (it compiles them without problems)

undefined reference to `ReadFromFile(int&, int&, int**&)'|

The question is, why does this not work?

The project's files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
main.cpp:

#include <iostream>
#include <fstream>
#include <vector>
#include "Read.h"

using namespace std;


int main()
{
    int n,m;
    int ** M = 0;
    ReadFromFile(n , m, M); //This is the line, where the linker complains
    for (int i = 0;i < n;i++)
    {
        delete [] M[i];
    }
    delete [] M;
    return 0;
}



Read.h:
1
2
3
4
5
6
7
8

#ifndef READ_H_INCLUDED
#define READ_H_INCLUDED


void ReadFromFile(int & n ,int & m , int ** &M);

#endif // READ_H_INCLUDED 



Read.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Read.h"
#include <iostream>

using namespace std;

void ReadFromFile(int & n ,int & m , int ** &M)
{
    ifstream f;
    string fn;
    bool hiba;
    cout << "A fajlnak a kovetkezokeppen kell kineznie: A szamokat szokozzel, vagy ujsor karakterrel kell elvalasztani. Az elso szam a kutyak szama, a masodik a kategoriak szama legyen. Utana pedig kutyankent fel kell sorolni, hogy melyik kategoriaban hany pontot kaptak." << '\n';
    do{
    hiba = false;
    cout << "Add meg a fajl eleresi helyet/nevet !" << '\n';
    cin >> fn;
    //A program megpróbálja megnyitni a fájlt.
    f.open(fn.c_str());
    if (f.fail())
    {
        f.clear();
        cout << "Nincs ilyen fajl !" << '\n';
        hiba = true;
        continue;
    }
    //Megpróbáljuk beolvasni az input adatok mennyiségét (ennek kell elsőnek lennie a fájlban).
    if (!(f >> n) || (n <= 0))
    {
        f.close();
        f.clear();
        cout << "A fajl nem megfelelo !" << '\n';
        hiba = true;
        continue;
    }

    if (!(f >> m) || (m <= 0))
    {
        f.close();
        f.clear();
        cout << "A fajl nem megfelelo !" << '\n';
        hiba = true;
        continue;
    }

    M = new int *[n];
    for (int i = 0;i < n;i++)
    {
        M[i] = new int[m];
    }

    //Megpróbáljuk vegigolvasni az inputsorozatot a fájlból.

    for (int i = 0;i < n;i++)
    {
        for (int j = 0;j < m;j++)
        {
            if ( !(f >> M[i][j]) || (M[i][j] < 0) || (M[i][j] > 10) )
                {
                    hiba = true;
                    cout << "A fajl nem megfelelo ! " << '\n';
                    break;
                }
        }
    }
    //Ha megvagyunk, bezárjuk a fájlt.
    f.close();
    f.clear();
    }while(hiba);
}





The problem is passing the third parameter by reference. You cannot create a reference to a null-pointer.

Check for example this link:

http://stackoverflow.com/questions/57483/difference-between-pointer-variable-and-reference-variable-in-c/57502#57502

Maybe transform your function to:
int ** ReadFromFile(int&, int&, int**&)

and your function call to:

 
int ** M = ReadFromFile(n , m); //This is the line, where the linker complains 


EDIT: typo
Last edited on
It seems you're not linking Read.obj with your program.
Maybe transform your function to:
int ** ReadFromFile(int&, int&, int**&)

and your function call to:

int ** M = ReadFromFile(n , m); //This is the line, where the linker complains


I'm still getting the same error.
onur
The problem is passing the third parameter by reference. You cannot create a reference to a null-pointer.


I think you are a bit confused..
Topic archived. No new replies allowed.