code workaround for friend ostream& operator<< (ostream&, Array<T>&);

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

 



Hello

something is wrong  with my  gcc  compiler

here is my code

===    snip  ===

template <class T>     // declare the template and the parameter
class Array            // the class being parameterized
{
public:
  // constructors
  Array(int itsSize = DefaultSize);
  Array(const Array &rhs);
  ~Array() { delete [] pType; }

  // operators
  Array& operator=(const Array&);
  T& operator[](int offSet) { return pType[offSet]; }
  const T& operator[](int offSet) const
      { return pType[offSet]; }
  // accessors
  int GetSize() const { return itsSize; }

  friend ostream& operator<< (ostream&, Array<T>&);

private:
  T *pType;
  int  itsSize;
};

template <class T>
ostream& operator<< (ostream& output, Array<T>& theArray)
{
  for (int i = 0; i<theArray.GetSize(); i++)
      output << "[" << i << "] " << theArray[i] << endl;
  return output;
}



== snip ===



g++ -ansi -pedantic -Wall -o ostream_operator_problem.out ostream_operator_problem.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++


g++ -o ostream_operator_problem.out ostream_operator_problem.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++



ostream_operator_problem.cpp:46: warning: friend declaration `std::ostream& operator<<(std::ostream&, Array<T>&)' declares a non-template function

ostream_operator_problem.cpp:46: warning: (if this is not what you intended,
make sure the function template has already been declared and add <> after
the function name here) -Wno-non-template-friend disables this warning
Undefined first referenced
symbol in file


operator<<(std::basic_ostream<char, std::char_traits<char> >&, Array<int>&)/var/tmp//ccMap1MM.o

ld: fatal: Symbol referencing errors. No output written to ostream_operator_problem.out

collect2: ld returned 1 exit status



Please help

gcc -v
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3 --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld --without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2



bash-2.05$ uname -X System = SunOS Node = Hirschgraben15-23 Release = 5.9 KernelID = Generic_112234-12 Machine = i86pc BusType = <unknown> Serial = <unknown> Users = <unknown> OEM# = 0 Origin# = 1 NumCPU = 1





best regards

Morten Gulbrandsen




// Using Operator ostream

#include <iostream>

using namespace std;

const int DefaultSize = 10;

class Animal
{
public:
	Animal(int);
	Animal();
	~Animal() {}
	int GetWeight() const { return itsWeight; }
	void Display() const { cout << itsWeight; }
private:
	int itsWeight;
};

Animal::Animal(int weight):
	itsWeight(weight)
{}

Animal::Animal():
	itsWeight(0)
{}

template <class T>     // declare the template and the parameter
class Array            // the class being parameterized
{
public:
	// constructors
	Array(int itsSize = DefaultSize);
	Array(const Array &rhs);
	~Array() { delete [] pType; }

	// operators
	Array& operator=(const Array&);
	T& operator[](int offSet) { return pType[offSet]; }
	const T& operator[](int offSet) const
		{ return pType[offSet]; }
	// accessors
	int GetSize() const { return itsSize; }

	friend ostream& operator<< (ostream&, Array<T>&);

private:
	T *pType;
	int  itsSize;
};

template <class T>
ostream& operator<< (ostream& output, Array<T>& theArray)
{
	for (int i = 0; i<theArray.GetSize(); i++)
		output << "[" << i << "] " << theArray[i] << endl;
	return output;
}

// implementations follow...

// implement the Constructor
template <class T>
Array<T>::Array(int size):
	itsSize(size)
{
	pType = new T[size];
	for (int i = 0; i<size; i++)
		pType[i] = 0;
}

// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
	itsSize = rhs.GetSize();
	pType = new T[itsSize];
	for (int i = 0; i<itsSize; i++)
		pType[i] = rhs[i];
}

// operator=
template <class T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
	if (this == &rhs)
		return *this;
	delete [] pType;
	itsSize = rhs.GetSize();
	pType = new T[itsSize];
	for (int i = 0; i<itsSize; i++)
		pType[i] = rhs[i];
	return *this;
}

int main()
{
	bool Stop = false;       // flag for looping
	int offset, value;
	Array<int> theArray;

	while (!Stop)
	{
		cout << "Enter an offset (0-9) ";
		cout << "and a value. (-1 to stop): " ;
		cin >> offset >> value;

		if (offset < 0)
			break;

		if (offset > 9)
		{
			cout << "***Please use values between 0 and 9.***\n";
			continue;
		}

		theArray[offset] = value;
	}

	cout << "\nHere's the entire array:\n";
	cout << theArray << endl;
	return 0;
}



/*

g++ -ansi -pedantic -Wall -o ostream_operator_problem.out ostream_operator_problem.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++


g++ -o ostream_operator_problem.out ostream_operator_problem.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++


ostream_operator_problem.cpp:46: warning: friend declaration `std::ostream& 
   operator<<(std::ostream&, Array<T>&)' declares a non-template function

ostream_operator_problem.cpp:46: warning: (if this is not what you intended, 
   make sure the function template has already been declared and add <> after 
   the function name here) -Wno-non-template-friend disables this warning
Undefined                       first referenced
 symbol                             in file

operator<<(std::basic_ostream<char, std::char_traits<char> >&, Array<int>&)/var/tmp//ccMap1MM.o

ld: fatal: Symbol referencing errors. No output written to ostream_operator_problem.out

collect2: ld returned 1 exit status






*/


/*
Your Comeau C/C++ test results are as follows:

Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing.  All rights reserved.
MODE:strict errors C++


In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link).


*/

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux