PDA

View Full Version : Function previously defined in created file?



Twey
13th January 2006, 21:51
When I do qmake -project;qmake;make on a project containing files with the Q_OBJECT macro, I get something like this:
moc_qtrackingbutton.cpp:80: error: redefinition of ‘void QTrackingButton::mouseMoved()’
qtrackingbutton.h:23: error: ‘void QTrackingButton::mouseMoved()’ previously defined here
make: *** [moc_qtrackingbutton.o] Error 1I've searched the internet a bit, and apparently putting the member functions in a different file to the class definition works. I did this, but it gave me "undefined reference to `vtable for classname'", until I moved these files out of the directory, then did qmake -project;qmake, then moved them back in for the final make. This is neither elegant nor convenient. Is there any way around, other than that?

wysota
13th January 2006, 22:34
First of all qmake -project should only be done once when you start working on a project. It creates a template for the project file, which then should be updated manualy. The error you experience is probably because you run qmake -project on a directory which already contains files generated by moc and these file are added to the SOURCES group but they shouldn't. So either make distclean before running qmake -project again or modify the project file yourself (which is the way to go).

Twey
14th January 2006, 12:49
Behold: no moc-generated files of any kind.
($:app4lin) ls -a
./ ../ app4lin.cxx qdoublebutton.h qtrackingbutton.hNow, after qmake -project:
($:app4lin) ls -a
./ ../ app4lin.cxx app4lin.pro qdoublebutton.h qtrackingbutton.hAll as expected. qmake and:
($:app4lin) ls -a
./ ../ app4lin.cxx app4lin.pro Makefile qdoublebutton.h qtrackingbutton.hAgain, all good. Then a make:
($:app4lin) make
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o app4lin.o app4lin.cxx
./qtrackingbutton.h:18: warning: unused parameter ‘e’
/usr/lib/qt-3.3/bin/moc qdoublebutton.h -o moc_qdoublebutton.cpp
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o moc_qdoublebutton.o moc_qdoublebutton.cpp
qtrackingbutton.h:18: warning: unused parameter ‘e’
/usr/lib/qt-3.3/bin/moc qtrackingbutton.h -o moc_qtrackingbutton.cpp
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o moc_qtrackingbutton.o moc_qtrackingbutton.cpp
qtrackingbutton.h:18: warning: unused parameter ‘e’
moc_qtrackingbutton.cpp:80: error: redefinition of ‘void QTrackingButton::mouseMoved()’
qtrackingbutton.h:23: error: ‘void QTrackingButton::mouseMoved()’ previously defined here
make: *** [moc_qtrackingbutton.o] Error 1My .pro file:
################################################## ####################
# Automatically generated by qmake (1.07a) Sat Jan 14 11:45:09 2006
################################################## ####################

TEMPLATE = app
INCLUDEPATH += .

# Input
HEADERS += qdoublebutton.h qtrackingbutton.h
SOURCES += app4lin.cxxNothing in SOURCES that shouldn't be.

yop
14th January 2006, 13:07
...apparently putting the member functions in a different file to the class definition works. I did this, but it gave me "undefined reference to `vtable for classname'", until I moved these files out of the directory, then did qmake -project;qmake, then moved them back in for the final make. This is neither elegant nor convenient. Is there any way around, other than that?Could you please be a little more descriptive. It should be easy to deal with this kind of error, but I really haven't totally undertood the problem.

Edit: As I can guess from the .pro file, you have implementation and declaration in the header file?
Edit2:Sorry I apparently pressed the forward button insted of edit while reading my previous post and I can't seem to find a way to delete the duplicate

Twey
14th January 2006, 13:24
As I can guess from the .pro file, you have implementation and declaration in the header file?Yes. Is this not correct? Here, I have cleaned all make- and qmake-generated files, moved my implementation to seperate .cxx files, and tried to build again.
($:app4lin) make
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o app4lin.o app4lin.cxx
./qtrackingbutton.cxx:20: warning: unused parameter ‘e’
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o qdoublebutton.o qdoublebutton.cxx
qtrackingbutton.cxx:20: warning: unused parameter ‘e’
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o qtrackingbutton.o qtrackingbutton.cxx
qtrackingbutton.cxx:20: warning: unused parameter ‘e’
/usr/lib/qt-3.3/bin/moc qdoublebutton.h -o moc_qdoublebutton.cpp
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o moc_qdoublebutton.o moc_qdoublebutton.cpp
qtrackingbutton.cxx:20: warning: unused parameter ‘e’
/usr/lib/qt-3.3/bin/moc qtrackingbutton.h -o moc_qtrackingbutton.cpp
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o moc_qtrackingbutton.o moc_qtrackingbutton.cpp
g++ -Wl,-rpath,/usr/lib/qt-3.3/lib -o app4lin app4lin.o qdoublebutton.o qtrackingbutton.o moc_qdoublebutton.o moc_qtrackingbutton.o -L/usr/lib/qt-3.3/lib -L/usr/X11R6/lib -lqt -lXext -lX11 -lm
qdoublebutton.o(.text+0x0): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/usr/lib/qt-3.3/include/qglist.h:150: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x0):/usr/lib/qt-3.3/include/qglist.h:150: first defined here
qdoublebutton.o(.text+0x3a): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:16: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x3a):./qtrackingbutton.cxx:16: first defined here
qdoublebutton.o(.text+0x74): In function `QTrackingButton::mouseMoved()':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:25: multiple definition of `QTrackingButton::mouseMoved()'
app4lin.o(.text+0x74):./qtrackingbutton.cxx:25: first defined here
qdoublebutton.o(.text+0x7a): In function `QTrackingButton::mouseMoveEvent(QMouseEvent*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:20: multiple definition of `QTrackingButton::mouseMoveEvent(QMouseEvent*)'
app4lin.o(.text+0x7a):./qtrackingbutton.cxx:20: first defined here
qdoublebutton.o(.text+0x80): In function `QDoubleButton::QDoubleButton(QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.cxx:3: multiple definition of `QDoubleButton::QDoubleButton(QWidget*, char*)'
app4lin.o(.text+0x80):./qdoublebutton.cxx:3: first defined here
qdoublebutton.o(.text+0x266): In function `QDoubleButton::QDoubleButton(QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.cxx:3: multiple definition of `QDoubleButton::QDoubleButton(QWidget*, char*)'
app4lin.o(.text+0x266):./qdoublebutton.cxx:3: first defined here
qdoublebutton.o(.text+0x44a): In function `QDoubleButton::clicked()':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.cxx:16: multiple definition of `QDoubleButton::clicked()'
app4lin.o(.text+0x4ee):./qdoublebutton.cxx:16: first defined here
qdoublebutton.o(.text+0x540): In function `QDoubleButton::moused()':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.cxx:21: multiple definition of `QDoubleButton::moused()'
app4lin.o(.text+0x5e4):./qdoublebutton.cxx:21: first defined here
qtrackingbutton.o(.text+0x0): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/usr/lib/qt-3.3/include/qglist.h:150: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x0):app4lin.cxx: first defined here
qtrackingbutton.o(.text+0x3a): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:16: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x3a):./qtrackingbutton.cxx:16: first defined here
qtrackingbutton.o(.text+0x74): In function `QTrackingButton::mouseMoved()':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:25: multiple definition of `QTrackingButton::mouseMoved()'
app4lin.o(.text+0x74):./qtrackingbutton.cxx:25: first defined here
qtrackingbutton.o(.text+0x7a): In function `QTrackingButton::mouseMoveEvent(QMouseEvent*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:20: multiple definition of `QTrackingButton::mouseMoveEvent(QMouseEvent*)'
app4lin.o(.text+0x7a):./qtrackingbutton.cxx:20: first defined here
moc_qdoublebutton.o(.text+0x0): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/usr/lib/qt-3.3/include/qglist.h:150: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x0):app4lin.cxx: first defined here
moc_qdoublebutton.o(.text+0x3a): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:16: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x3a):./qtrackingbutton.cxx:16: first defined here
moc_qdoublebutton.o(.text+0x74): In function `QTrackingButton::mouseMoved()':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:25: multiple definition of `QTrackingButton::mouseMoved()'
app4lin.o(.text+0x74):./qtrackingbutton.cxx:25: first defined here
moc_qdoublebutton.o(.text+0x7a): In function `QTrackingButton::mouseMoveEvent(QMouseEvent*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.cxx:20: multiple definition of `QTrackingButton::mouseMoveEvent(QMouseEvent*)'
app4lin.o(.text+0x7a):./qtrackingbutton.cxx:20: first defined here
moc_qtrackingbutton.o(.text+0x286): In function `QTrackingButton::mouseMoved()':
/home/twey/devel/cpp/qt/hts/app4lin/moc_qtrackingbutton.cpp:80: multiple definition of `QTrackingButton::mouseMoved()'
app4lin.o(.text+0x74):./qtrackingbutton.cxx:25: first defined here
/usr/bin/ld: Warning: size of symbol `QTrackingButton::mouseMoved()' changed from 5 in app4lin.o to 31 in moc_qtrackingbutton.o
collect2: ld returned 1 exit status
make: *** [app4lin] Error 1Project file:
################################################## ####################
# Automatically generated by qmake (1.07a) Sat Jan 14 12:22:56 2006
################################################## ####################

TEMPLATE = app
INCLUDEPATH += .

# Input
HEADERS += qdoublebutton.h qtrackingbutton.h
SOURCES += app4lin.cxx qdoublebutton.cxx qtrackingbutton.cxx
/EDIT: there really ought to be scrollbars on those [quote] boxes.

yop
14th January 2006, 13:43
Yes. Is this not correct? No it's perfectly legitimate that's not the problem, I was just thinking of various things for what happens, but I still can't visualize it. When I said that you should be more descriptive I meant for the workaround you found that didn't fit your needs. If it's possible attach your sourcecode to duplicate the error.

There really ought to be scrollbars on those [Q U O T E] boxes.You can use the [C O D E] (without the spaces of cource) tag instead to get scrollbars

Twey
14th January 2006, 14:27
The workaround was to keep the implementation out of both SOURCES and HEADERS groups.
Source attached.

yop
14th January 2006, 18:00
From the assistant (Signals and Slots)

Signals are automatically generated by the moc and must not be implemented in the .cpp file. Remove the implementation of your mouseMoved() and you' re done ;)

Edit: Also rename this slot void QDoubleButton::clicked(). It's got the same name as the signal

Twey
14th January 2006, 18:08
Aha :) I did find the first problem, shortly after posting, but not the second.
Nevertheless, I still get these:
moc_qdoublebutton.o(.text+0x0): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/usr/lib/qt-3.3/include/qglist.h:150: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x0):/usr/lib/qt-3.3/include/qglist.h:150: first defined here
moc_qdoublebutton.o(.text+0x3a): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:17: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x3a):/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:17: first defined here
moc_qdoublebutton.o(.text+0x74): In function `QTrackingButton::mouseMoveEvent(QMouseEvent*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:21: multiple definition of `QTrackingButton::mouseMoveEvent(QMouseEvent*)'
app4lin.o(.text+0x74):/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:21: first defined here
moc_qdoublebutton.o(.text+0x304): In function `QDoubleButton::clicked()':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:31: multiple definition of `QDoubleButton::clicked()'
app4lin.o(.text+0x4ee):/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:31: first defined here
moc_qdoublebutton.o(.text+0x3fa): In function `QDoubleButton::moused()':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:36: multiple definition of `QDoubleButton::moused()'
app4lin.o(.text+0x5e4):/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:36: first defined here
moc_qdoublebutton.o(.text+0x556): In function `QDoubleButton::QDoubleButton(QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:18: multiple definition of `QDoubleButton::QDoubleButton(QWidget*, char*)'
app4lin.o(.text+0x7e):/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:18: first defined here
/usr/bin/ld: Warning: size of symbol `QDoubleButton::QDoubleButton(QWidget*, char*)' changed from 488 in app4lin.o to 484 in moc_qdoublebutton.o
moc_qdoublebutton.o(.text+0x73a): In function `QDoubleButton::QDoubleButton(QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:18: multiple definition of `QDoubleButton::QDoubleButton(QWidget*, char*)'
app4lin.o(.text+0x266):/home/twey/devel/cpp/qt/hts/app4lin/qdoublebutton.h:18: first defined here
/usr/bin/ld: Warning: size of symbol `QDoubleButton::QDoubleButton(QWidget*, char*)' changed from 484 in app4lin.o to 488 in moc_qdoublebutton.o
moc_qtrackingbutton.o(.text+0x364): In function `QTrackingButton::mouseMoveEvent(QMouseEvent*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:21: multiple definition of `QTrackingButton::mouseMoveEvent(QMouseEvent*)'
app4lin.o(.text+0x74):/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:21: first defined here
moc_qtrackingbutton.o(.text+0x0): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
moc_qtrackingbutton.cpp: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x0):app4lin.cxx: first defined here
moc_qtrackingbutton.o(.text+0x3a): In function `QTrackingButton::QTrackingButton(QString, QWidget*, char*)':
/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:17: multiple definition of `QTrackingButton::QTrackingButton(QString, QWidget*, char*)'
app4lin.o(.text+0x3a):/home/twey/devel/cpp/qt/hts/app4lin/qtrackingbutton.h:17: first defined here

anda_skoa
14th January 2006, 18:19
Looks like missing include guards, i.e.



#ifndef SOMEHEADER_H
#define SOMEHEADER_H

class SomeClass
{
};

#endif


Cheers,
_

Twey
14th January 2006, 19:05
Nope, got them.

yop
14th January 2006, 19:16
The attached project built fine with:
g++ --version
g++ (GCC) 3.3.5 20050117 (prerelease) (SUSE Linux)
qmake --version
Qmake version: 1.07a (Qt 3.3.4)
under Suse Linux 9.3
A diff with your files will be usefull

Twey
14th January 2006, 20:11
Yes, builds here too. diff attached.

/EDIT: Of more interest might be this diff between your project and the same project when declaration and implementation are cat'ed into one file, which gives the same errors as above.

yop
14th January 2006, 20:42
Apparently that was the problem ;)

Twey
14th January 2006, 21:00
Is this [putting definition and implementation in the same file] not correct?No it's perfectly legitimate that's not the problemIf that's so, I don't understand why it should be a problem :-\

yop
14th January 2006, 21:17
Yes it's food for thought, I've been googling around for it but I can't seem to find any answers :(. It was a trial and error process for me too. It really felt right to have it in the header file (I would have told you to remove them in the first place) but apparenty it's not.

jacek
14th January 2006, 21:19
If that's so, I don't understand why it should be a problem :-\
In such case you must add a hint for qmake that it should run moc program on your .cpp file, as it only checks headers for Q_OBJECT.

You have to add:
#include "filename.moc"at the end of a file.

Edit: OK, forget it. I should check your program before posting. The problem is different.

Twey
14th January 2006, 21:22
In such case you must add a hint for qmake that it should run moc program on your .cpp file, as it only checks headers for Q_OBJECT.But the files were headers.
Also, yop, when the second button in your code (b2) is connected to the moused() slot, nothing happens.

yop
14th January 2006, 21:23
In such case you must add a hint for qmake that it should run moc program on your .cpp file, as it only checks headers for Q_OBJECT.

You have to add:
#include "filename.moc"at the end of a file.
Don't you get a vtable blah blah error in that case? There is an undefined reference error here. Ok I'll shut up and reboot linux to check it out myself...


Edit: OK, forget it. I should check your program before posting. The problem is different.Edit: Ok no need to check it out then :)

yop
14th January 2006, 21:25
Also, yop, when the second button in your code (b2) is connected to the moused() slot, nothing happens. I only tried to compile not add any functionality ;)

jacek
14th January 2006, 21:39
But the files were headers.
Also, yop, when the second button in your code (b2) is connected to the moused() slot, nothing happens.
Sorry, I've confused this thread with another one.

The problem is that you have placed code in header files. You can only put declarations, templates and inline functions there (unless you want linking errors). Compiler doesn't have to know anything about .h files --- preprocessor creates a single .cpp file with all headers inside it and then it feeds such file to compiler.

In this case compiler put code of those methods in both moc_qtrackingbutton.o and app4lin.o, because both moc_qtrackingbutton.cpp and app4lin.cpp include qtrackingbutton.h.

jacek
14th January 2006, 21:47
Don't you get a vtable blah blah error in that case?
No, it should prevent it. I was thinking about a bit different situation --- when you place class definition in .cpp file and it has Q_OBJECT macro inside it, qmake won't notice that and you will have a "undefined reference to vtable" error, like here. (http://www.qtcentre.org/forum/showpost.php?p=1149&postcount=18)

Twey
14th January 2006, 21:58
I only tried to compile not add any functionality ;)
I beg your pardon: I missed a statement.

In this case compiler put code of those methods in both moc_qtrackingbutton.o and app4lin.o, because both moc_qtrackingbutton.cpp and app4lin.cpp include qtrackingbutton.h.So one can't include custom headers containing Q_OBJECT in a header. OK.

yop
14th January 2006, 22:25
No, it should prevent it. I was thinking about a bit different situation --- when you place class definition in .cpp file and it has Q_OBJECT macro inside it, qmake won't notice that and you will have a "undefined reference to vtable" error, like here. (http://www.qtcentre.org/forum/showpost.php?p=1149&postcount=18)
Yeah it's what I meant that #include foo.moc handles the "vtable blah blah" errors but that was not the case. The solution was clear as you posted above while I was thinking of... well I don't really know what I was thinking I got a bit confused :o

jacek
15th January 2006, 00:36
So one can't include custom headers containing Q_OBJECT in a header. OK.
No, you can do this. The problem you have encountered has nothing to do with Q_OBJECT.

Suppose you have such project:
// a.h
#ifndef __A_H__
#define __A_H__

class A
{
public:
virtual int foo();
};

int A::foo() { return 10; }

#endif

// b.h

#ifndef __B_H__
#define __B_H__

#include "a.h"

class B : public A
{
public:
int foo();
};

#endif

// b.cpp

#include "b.h"

int B::foo() { return 20; }

// main.cpp

#include <iostream>
#include "a.h"
#include "b.h"

int main()
{
A a;
B b;
std::cout << a.foo() << b.foo() << std::endl;
}As you see there are two .cpp files and to obtain the executable you must compile each of those files and link them toghether.

First those files will be go through preprocessor that will turn them to something like this:
// main.cpp
<code from iostream header>

class A
{
public:
virtual int foo();
};

int A::foo() { return 10; }

class B : public A
{
public:
virtual int foo();
};

int main()
{
A a;
B b;
std::cout << a.foo() << b.foo() << std::endl;
}

// b.cpp

class A
{
public:
virtual int foo();
};

int A::foo() { return 10; }

class B : public A
{
public:
virtual int foo();
};

int B::foo() { return 20; }

As you can see both of these files contain the implementation of the A::foo() method. So after compilation both main.o and b.o files will have the code for that method and linker will raise an error as it won't be able to determine which one is valid.


b.o(.text+0x0): In function `A::foo()':
b.cpp: multiple definition of `A::foo()'
main.o(.text+0x0):main.cpp: first defined here
collect2: ld returned 1 exit status

To solve the problem you should either make A::foo() an inline method or move it to a.cpp file.

Twey
15th January 2006, 01:53
Ah, I see. OK.