[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C++: Problem with overloading a constructor when splitting a src file.
- To: Shaul Karl <shaulka(at-nospam)bezeqint.net>
- Subject: Re: C++: Problem with overloading a constructor when splitting a src file.
- From: guy keren <choo(at-nospam)actcom.co.il>
- Date: Sun, 18 Nov 2001 01:50:13 +0200 (EET)
- cc: <linux-il(at-nospam)linux.org.il>
- Delivered-To: linux.org.il-linux-il@linux.org.il
- In-Reply-To: <E165F2K-0004NA-00@rakefet>
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
you have put an inline function inside a '.cc' file. since it is inline,
it will NOT be included in the object file 'base.cc', and thus, during
link, there base constructor will be undefined. this is your bug - not
g++'s.
fixes:
1. move the inline function into the header file.
2. make the function not 'inline'.
guy
On Sun, 18 Nov 2001, Shaul Karl wrote:
> Date: Sun, 18 Nov 2001 01:38:10 +0200
> From: Shaul Karl <shaulka@bezeqint.net>
> To: linux-il@linux.org.il
> Subject: C++: Problem with overloading a constructor when splitting a
> src file.
>
> Part 1: this works as expected
> ------------------------------
>
> [01:27:40 tmp]$ cat main.cc
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> class base
> {
> public:
> base();
> base(string &str);
> };
>
> class derived : public base
> {
> public:
> derived(string &str) : base(str) {}
> };
>
> inline base::base() {}
>
> inline base::base(string &str)
> {
> cout << str << endl;
> }
>
>
> int main(void)
> {
> string str("test succeeded.");
> derived testingDerived(str);
> }
>
> [01:27:45 tmp]$ g++-3.0 -Wall -o main main.cc
> main.cc: In function `int main()':
> main.cc:30: warning: unused variable `derived testingDerived'
> [01:29:10 tmp]$
>
>
>
> Part 2: Why this does not work?
> -------------------------------
>
> Next I have tried to split this into 3 files:
>
> [01:19:08 tmp]$ more *.h *.cc
> ::::::::::::::
> header.h
> ::::::::::::::
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> class base
> {
> public:
> base();
> base(string &str);
> };
>
> class derived : public base
> {
> public:
> derived(string &str) : base(str) {}
> };
> ::::::::::::::
> base.cc
> ::::::::::::::
> #include "header.h"
>
> inline base::base() {}
>
> inline base::base(string &str)
> {
> cout << str << endl;
> }
> ::::::::::::::
> main.cc
> ::::::::::::::
> #include "header.h"
>
> int main(void)
> {
> string str("test succeeded.");
> derived testingDerived(str);
> }
>
>
> [01:19:40 tmp]$ for f in *.cc; do C="g++-3.0 -Wall -c -o ${f%.*}.o $f";
> echo $C;$($C); done; C="g++-3.0 -Wall -o main main.o base.o"; echo $C;
> $($C);
> g++-3.0 -Wall -c -o base.o base.cc
> g++-3.0 -Wall -c -o main.o main.cc
> main.cc: In function `int main()':
> main.cc:6: warning: unused variable `derived testingDerived'
> g++-3.0 -Wall -o main main.o base.o
> main.o: In function `derived::derived(std::string&)':
> main.o(.gnu.linkonce.t._ZN7derivedC1ERSs+0x10): undefined reference to
> `base::base(std::string&)'
> collect2: ld returned 1 exit status
> [01:20:53 tmp]$
=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il