[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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]$


Why it does not work?
Is this reproducible with other distros or g++ versions?


[01:20:53 tmp]$ g++-3.0 -v
Reading specs from /usr/lib/gcc-lib/i386-linux/3.0.2/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,p
roto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man 
--enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib 
--enable-long-long --enable-nls --without-included-gettext 
--disable-checking --enable-threads=posix --enable-java-gc=boehm 
--with-cpp-install-dir=bin --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.0.2 20010922 (Debian prerelease)
[01:23:23 tmp]$ 
-- 

    Shaul Karl
    email: shaulka (replace these parenthesis with @) bezeqint,
           delete the comma and the white space characters and add .net



=================================================================
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