Quick question

This is a discussion on Quick question within the C++ Programming forums, part of the General Programming Boards category; Hello all: I have been working on a project in my spare time and have been unable to find the ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Quick question

    Hello all:

    I have been working on a project in my spare time and have been unable to find the source of a linker error after a few hours.

    First, the overloaded output operator in this class is throwing the following error when attempting to use the << operator:
    Code:
    1>Linking...
    1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & 
      __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CayleyTable<int> &)" 
      (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$CayleyTable@H@@@Z) 
      referenced in function _main
    1>C:\Documents and Settings\asdfg\My Documents\Visual Studio 2005\Projects\test2\Debug\test2.exe : 
      fatal error LNK1120: 1 unresolved externals
    This is with the following class:
    Code:
    template <class ItemType>
    class CayleyTable
    {
    public:
    	CayleyTable();
    	CayleyTable(int NumOfElements);
    	CayleyTable(CayleyTable<ItemType>& AnotherCayleyTable);
    
    	void SetNumberOfElements(int n);
    	void DefineCayleyTable(vector< vector<ItemType> >& ACayleyTable);
    
    	int NumberOfElements();
    
    	list< OrderedPair<int> >& FindIndices(ItemType Item);
    	//Post: returns a list of ordered pairs of integers representing the indices in 
            //  which Item was found in the Cayley table
    
    	ItemType ReturnIndex(int i, int j);
    	//Post: returns the (i,j)th entry of the Cayley table
    
    	friend ostream& operator << (ostream& out, CayleyTable<ItemType>& TheTable);
    
    private:
    	vector< vector<ItemType> > TheCayleyTable;
    };
    I defined the output operator for this class as follows:
    Code:
    template <class ItemType>	
    ostream& operator << (ostream& out, CayleyTable<ItemType>& TheTable)
    {
    	for (int i = 0; i < TheTable.TheCayleyTable.size(); i++)
    	{
    		for (int j = 0; j < TheTable.TheCayleyTable.size(); j++)
    		{
    			out << TheTable.TheCayleyTable[i][j] << "  ";
    		}
    		out << endl;
    	}
    
    	return out;
    }
    Thanks for any help!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Put your template operator << in a header and don't prototype it and see if that works. If it isn't already, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Did you put the implementation of this operator in a header file, as is necessary for templates?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you put your output operator in the header file? It needs to be included when you compile the source using the template.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Yes, all of the code associated with the class is in CayleyTable.h

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears that you might need to declare the friend as this:
    Code:
        template <class T>
    	friend ostream& operator << (ostream& out, CayleyTable<T>& TheTable);
    It seems that the class template typename doesn't apply to the declaration of a free friend function. (Compiler warns about declaring a non-templated function otherwise.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Quote Originally Posted by anon View Post
    It appears that you might need to declare the friend as this:
    Code:
        template <class T>
    	friend ostream& operator << (ostream& out, CayleyTable<T>& TheTable);
    It seems that the class template typename doesn't apply to the declaration of a free friend function. (Compiler warns about declaring a non-templated function otherwise.)
    Thanks, that did it.

    Time for some sleep now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21