Toon posts:

[C++] Undefined referenced bij aanroep static method.

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hallo,

ik heb de volgende code en makefile. Het probleem is dat ik het niet geompileerd krijg wanneer ik in de main de static method van Test aanroep; Test::setInt(1); dus. Ik krijg dan steeds de volgende error:
g++ -c -g -Wall test.h main.cpp
g++ -g -o test test.o main.o
main.o(.text+0x12e): In function `main':
/home/joren/temp/main.cpp:10: undefined reference to `Test::setInt(int)'
collect2: ld returned 1 exit status
make: *** [test] Error 1

Het lijkt me aan de makefile te liggen, dus hopelijk kan hier iemand zeggen wat er ontbreekt of verkeerd staat in de makefile.

test.h
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef MYTEST
#define MYTEST

#include <cstdio>
#include <iostream>

class Test
{
public:
    Test(){}
    ~Test(){}
    static void setInt(int i);
private:
    static int m_int;   
};


test.cpp
code:
1
#include "test.h"


main.cpp
code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <iostream>
#include "test.h"

using namespace std;

int main(int argc, char *args[])
{
    printf("blabla\n");
    Test::setInt(1);
    return(0);
}


makefile
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Options for development
CC=g++
CFLAGS=-c -g -Wall 

test: main.o test.o
    $(CC) -g -o test test.o main.o

main.o: test.h main.cpp
    $(CC) $(CFLAGS) test.h main.cpp

test.o: test.h test.cpp
    $(CC) $(CFLAGS) test.cpp

clean: 
    rm -f *.o *.tab.c *.tab.h *.output *.yy.cpp *~ *.output y.tab.* parse

  • Daspeed
  • Registratie: Maart 2001
  • Laatst online: 23:41
Ik kan het verkeerd hebben, maar mis je geen #endif in je headerfile?
Edit: en owja waar is je implementatie van de class Test en dan met name de methode setInt

[ Voor 40% gewijzigd door Daspeed op 22-05-2005 11:14 ]


  • Super_ik
  • Registratie: Maart 2001
  • Laatst online: 18:48

Super_ik

haklust!

je moet eerst iets aanmaken als Test volgens mij
code:
1
2
Test test;
test.setInt(1)

in je main ?

8<------------------------------------------------------------------------------------
Als ik zo door ga haal ik m'n dood niet. | ik hou van goeie muziek


  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 06-05 18:51

Creepy

Tactical Espionage Splatterer

Er mist een #endif in je test.h, of ben je dat vergeten hier neer te zetten? Ook zie ik in je compiler output niet dat test.o wordt gecompileerd. Ik zou erop gokken dat dat komt doordat je nu een test.o hebt en test als programma. Kijk eens of het werkt door je programma een andere naam te geven.

code:
1
2
testprog: main.o test.o
    $(CC) -g -o testprog test.o main.o

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • whoami
  • Registratie: December 2000
  • Laatst online: 23:00
Super_ik schreef op zondag 22 mei 2005 @ 11:16:
je moet eerst iets aanmaken als Test volgens mij
code:
1
2
Test test;
test.setInt(1)

in je main ?
Nee, want het is een static.

https://fgheysels.github.io/


  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 06-05 18:51

Creepy

Tactical Espionage Splatterer

DrClearbottom schreef op zondag 22 mei 2005 @ 11:12:
Edit: en owja waar is je implementatie van de class Test en dan met name de methode setInt
:D Daar keek ik nog ff overheen. Vergeet m'n vorige post maar ;)

[ Voor 7% gewijzigd door Creepy op 22-05-2005 11:21 ]

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • Jrz
  • Registratie: Mei 2000
  • Laatst online: 07-05 00:35

Jrz

––––––––––––

waar is je setInt() code?

Ennnnnnnnnn laat losssssssss.... https://github.com/jrz/container-shell (instant container met chroot op current directory)


  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 09-04 22:08
Test is een erg onhandige naam in Linux shells.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


  • Super_ik
  • Registratie: Maart 2001
  • Laatst online: 18:48

Super_ik

haklust!

MSalters schreef op zondag 22 mei 2005 @ 11:29:
Test is een erg onhandige naam in Linux shells.
want?

* Super_ik is nieuwsgierig :p

edit:

nevermind, man test is duidelijk genoeg :)

[ Voor 15% gewijzigd door Super_ik op 22-05-2005 11:36 ]

8<------------------------------------------------------------------------------------
Als ik zo door ga haal ik m'n dood niet. | ik hou van goeie muziek


  • MrBucket
  • Registratie: Juli 2003
  • Laatst online: 29-10-2022
Precies, in je test.cpp moet je nog iets hebben staan als
C++:
1
2
3
void Test::setInt(int i){
    m_int = i;
}

[ Voor 2% gewijzigd door MrBucket op 23-05-2005 13:12 . Reden: 'void' vergeten :X ]


Verwijderd

Topicstarter
Hallo,

dank je voor jullie reacties!! Maar het probleem zit hem neit in de implementatie van de setInt(int i), dat is gewoon iets wat ik ff vergeten had, die implementatie zit in de h file, moet dus zijn:

[code]
static void setInt(int i) { m_int = i; }
[code/]


De fout zit dus ergens in de makefile vlg mij, geen implementatie fout volgens mij... Probeer het desnoods zelf eens te compileren.... als jullie tijd hebben :-)
Het probleem dat ik heb is puur bij het aanroepen van de static method...;

Groeten.

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 15-04 22:07

NMe

Quia Ego Sic Dico.

Waar in de h-file staat die implementatie? In de klassedefinitie of daaronder?

C++:
1
2
3
4
class Blaat {
  public:
    static void setInt(int i) { m_int = i; }
};

of:
C++:
1
2
3
4
5
class Blaat {
  // .....
};

static void setInt(int i) { m_int = i; }

In het laatste geval is het natuurlijk fout. Bij mijn weten moet je daar wel de klassenaam voor zetten:
C++:
1
static void Test::setInt(int i) { m_int = i; }

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • H!GHGuY
  • Registratie: December 2002
  • Niet online

H!GHGuY

Try and take over the world...

ik had overlaatst ook een erg verwant probleem:

C++:
1
2
3
4
5
6
7
8
9
class Test
{
  static void setInt( int i );
public: // methodes
private: // nog wat meuk
  static int getal;
};

void Test::setInt(int i) { getal = i };


de fout die ik kreeg: undefined external int Test::getal

ASSUME makes an ASS out of U and ME


  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 09-04 22:08
-NMe- schreef op zondag 22 mei 2005 @ 21:20:
Waar in de h-file staat die implementatie? In de klassedefinitie of daaronder?

C++:
1
2
3
4
class Blaat {
  public:
    static void setInt(int i) { m_int = i; }
};

of:
C++:
1
2
3
4
5
class Blaat {
  // .....
};

static void setInt(int i) { m_int = i; }

In het laatste geval is het natuurlijk fout. Bij mijn weten moet je daar wel de klassenaam voor zetten:
C++:
1
static void Test::setInt(int i) { m_int = i; }
Nee, static mag alleen op de in-class declaratie. Overigens mag je niet zomaar een functie definieren in een header.

De code boven doet allebei fout, met als gevolg dat je iets heel anders "legaals" krijgt: 1 functie ::setInt per .cpp file die de .h include. Die functies zijn allemaal verschillend, dus &::setInt != ::setInt. Omdat het allemaal verschillende definities zijn, heb je dus ook geen multiple definition linker fout.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 01:10

.oisyn

Moderator Devschuur®

Demotivational Speaker

HIGHGuY schreef op maandag 23 mei 2005 @ 10:27:
ik had overlaatst ook een erg verwant probleem:

C++:
1
2
3
4
5
6
7
8
9
class Test
{
  static void setInt( int i );
public: // methodes
private: // nog wat meuk
  static int getal;
};

void Test::setInt(int i) { getal = i };


de fout die ik kreeg: undefined external int Test::getal
Omdat je 'getal' alleen hebt gedeclareerd maar nergens gedefinieerd. Je zult toch ruimte moeten reserveren voor die int in een bepaalde translation unit, net als met globale variabelen.

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • MrBucket
  • Registratie: Juli 2003
  • Laatst online: 29-10-2022
Voor de volledigheid, zo hoort het dus:

MyClass.h:
C++:
1
2
3
4
5
class MyClass{
  static int s_iValue;
  public:
    static void setValue(int i);
};


MyClass.cpp:
C++:
1
2
3
4
5
6
7
include "MyClass.h"

int MyClass::s_iValue = 0;

void MyClass::setValue(int i){
  s_iValue = i;
}

  • H!GHGuY
  • Registratie: December 2002
  • Niet online

H!GHGuY

Try and take over the world...

een heele luide "aaaaaahhh" :P

zo hoort dat dus in c++ met die static variabelen in classes ;) thx

ASSUME makes an ASS out of U and ME


Verwijderd

Topicstarter
Hallo,

Thx. De laatste reply is volgens mij wat ik zoek, ik zal het vanavond eens proberen en hopelijk krijg ik het dan gecompileerd.

Thx voor alle replies.
Pagina: 1