• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
C++ Templates compilation problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++ Templates compilation problem


  • Subject: C++ Templates compilation problem
  • From: Damian Wilson <email@hidden>
  • Date: Thu, 10 Feb 2011 17:14:36 +0000
  • Importance: Normal

Hi Xcode-users

I'm having an issue compiling some source code that I've ported over from Windows. It's one of those things where I've spent a long time staring at it and can't work out why it doesn't like it.

I'm building on an iMac, Xcode version 3.2.5, GNU 4.2.1.

Here is the code:
#include <string>

template<class T>
class CompoundObject
{
public:
CompoundObject(int obj1, T* obj2)
: m_object1(obj1), m_object2(obj2) {}

int m_object1;
T* m_object2;
};

class Streamer
{
public:
Streamer() {}
~Streamer() {}

template<class T> Streamer& operator>>(CompoundObject<T>& c)
{
printf("Obj1 = %d\n", c.m_object1);
return *this;
}
};

template<class T> inline 
CompoundObject<T> MakeCompoundObject(int obj1, T* obj2)
{
return CompoundObject<T>(obj1, obj2);
}

int _tmain(int argc, _TCHAR* argv[])
{
Streamer s;
std::string object("Hello");
s >> MakeCompoundObject(1, &object);  <== ERROR
return 0;
}

The error returned is:
/Volumes/Development/TemplateSample/TemplateSample.cpp: In function 'int _tmain(int, char**)':
/Volumes/Development/TemplateSample/TemplateSample.cpp:47: error: no match for 'operator>>' in 's >> MakeCompoundObject [with T = std::string](1, (& object))'
/Volumes/Development/TemplateSample/TemplateSample.cpp:29: note: candidates are: Streamer& Streamer::operator>>(CompoundObject<T>&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]

The weird thing about the error is it identifies the correct candidate function, but is then refusing to use it. If I create a temporary variable to hold the result of MakeCompoundObject, and then call "s >> temporary", it compiles fine but that is rather spoiling the brevity of some of our code.

Any ideas?

Thanks in advance
Damian

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: C++ Templates compilation problem
      • From: James Walker <email@hidden>
  • Prev by Date: Re: Adding a target dependency
  • Next by Date: Re: C++ Templates compilation problem
  • Previous by thread: Re: Adding a target dependency
  • Next by thread: Re: C++ Templates compilation problem
  • Index(es):
    • Date
    • Thread