delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/12/03/20:18:23

From: Farooq Azam <fazam AT vt DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: [HELP]: Template Question
Date: Sun, 03 Dec 2000 19:13:48 -0500
Organization: Virginia Tech, Blacksburg, Virginia, USA
Lines: 93
Message-ID: <3A2AE1BC.CEE72E58@vt.edu>
NNTP-Posting-Host: h80ad2791.async.vt.edu
Mime-Version: 1.0
X-Trace: solaris.cc.vt.edu 975888795 26648 128.173.39.145 (4 Dec 2000 00:13:15 GMT)
X-Complaints-To: abuse AT vt DOT edu
NNTP-Posting-Date: 4 Dec 2000 00:13:15 GMT
X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U)
X-Accept-Language: en,zh-CN
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello,
I need some help/sugegstions with compilation of the following code. It
compiles fine under MS-VC++ 6  and does not under g++ version 2.952.

Thanks in advance for any suggestions.

    -Farooq


-- Errors--
D:\DOWNLO~1\TEMPLA~1>g++ -o testoperator test.cpp
In file included from testimpl.cpp:1,
                 from test.cpp:1:
testspec.h:8: warning: friend declaration `class ostream & operator
<<(ostream & , const Complex<T> &)'
testspec.h:8: warning:   declares a non-template function
testspec.h:8: warning:   (if this is not what you intended, make sure
testspec.h:8: warning:   the function template has already been
declared,
testspec.h:8: warning:   and add <> after the function name here)
testspec.h:8: warning:   -Wno-non-template-friend disables this warning.

d:/djgpp/tmp\ccJvza2q.o(.text+0x38):test.cpp: undefined reference to
`operator<<
(ostream &, Complex<int> const &)'
d:/djgpp/tmp\ccJvza2q.o(.text+0x5f):test.cpp: undefined reference to
`operator<<
(ostream &, Complex<int> const &)'
collect2: ld returned 1 exit status
---

--- Code: test.cpp ---
#include "testimpl.cpp"
int main(void)
{
        int a=2;
        int b=3;

        Complex<int> C1(a,b);
        cout << C1;
        Complex<int>C2(C1);
        cout << C2;
        return 0;
}
---

--- Code: testspec.h ---
#ifndef _TESTSPEC_H
#define _TESTSPEC_H
#include <iostream.h>
#include <stdlib.h>

template<class T> class Complex
{
        friend ostream& operator << (ostream &output, const Complex<T>
&complex);
        public:
                Complex(T real, T imaginary);
                Complex(const Complex<T> &complex);
        private:
                T real;
                T imaginary;
};

#endif
---
--- Code: testimpl.cpp ---
#include "testspec.h"

template<class T> Complex<T>::Complex(T nreal, T nimaginary)
{
        real=nreal;
        imaginary=nimaginary;
}

template<class T> Complex<T>::Complex(const Complex<T> &complex)
{
        real=complex.real;
        imaginary=complex.imaginary;
}

template<class T> ostream &operator << (ostream &output, const
Complex<T> &complex)
{
        output << "Complex Number..." << endl;
        output << "Real part .. " << complex.real << endl;
        output << "Imaginary part ..." << complex.imaginary << endl;
        output << endl;
        return output;
}

---

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019