Thread: Very strange compiling bug

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Very strange compiling bug

    I can't seem to figure this out. For some reason i get no errors at all when i do this:

    Code:
    ByteStream packet = PacketInt1(UNLIMITEDSIG);
    But when i do this:

    Code:
    ByteStream packet(PacketInt1(UNLIMITEDSIG));
    I get a whole bunch of errors associated with the "packet" object.

    Both of these statements should call this constructor:
    Code:
    template <class T> ByteStream(const T& data);
    This is the errors i get when i call the second method:

    Code:
    G:\GServerEmulator\src\Player.cpp: In member function `void Player::InitializeClient()':
    G:\GServerEmulator\src\Player.cpp:138: error: assignment of function `ByteStream packet(PacketInt1)'
    G:\GServerEmulator\src\Player.cpp:138: error: cannot convert `PacketInt1' to `ByteStream ()(PacketInt1)' in assignment
    G:\GServerEmulator\src\Player.cpp:142: error: invalid operands of types `ByteStream ()(PacketInt1)' and `const char[2]' to binary `operator<<'
    G:\GServerEmulator\src\Player.cpp:147: error: assignment of function `ByteStream packet(PacketInt1)'
    G:\GServerEmulator\src\Player.cpp:147: error: cannot convert `PacketInt1' to `ByteStream ()(PacketInt1)' in assignment
    G:\GServerEmulator\src\Player.cpp:152: error: no match for 'operator<<' in 'packet << PacketInt1(((int)i))'
    G:\GServerEmulator\src\/ByteStream.h: In constructor `ByteStream::ByteStream(const T&) [with T = ByteStream ()(PacketInt1)]':
    G:\GServerEmulator\src\Player.cpp:122:   instantiated from here
    G:\GServerEmulator\src\/ByteStream.h:91: error: invalid application of `sizeof' to a function type
    G:\GServerEmulator\src\/ByteStream.h:92: error: invalid application of `sizeof' to a function type
    Heres the full code for that function:

    Code:
    void Player::InitializeClient()
    {
        //Allow more than 8 players
        ByteStream packet(PacketInt1(UNLIMITEDSIG));
        //PacketInt1(73);
        WritePacket(packet);
    
        if(Account::loadOnly)
        {
            Player* duplicatePlayer = server.FindPlayer(Account::name);
    
            if(duplicatePlayer)
            {
                //To do:
            }
        }
    
    
        if(clientType == CLIENTPLAYER)
        {
            //Send staff guild tags
            packet = PacketInt1(SSTAFFGUILDS);
            const vector<string>& staffGuilds = server.GetStaffGuilds();
    
            for(unsigned int i = 0; i < staffGuilds.size(); ++i)
                packet << "\"" << staffGuilds[i] << "\",";
    
            WritePacket(packet);
    
            //Send the players properties
            packet = PacketInt1(SPLAYERPROPS);
    
            for(unsigned int i = 0; i < PROPS_COUNT; ++i)
            {
                if (sendInit[i])
                    packet << PacketInt1(i) << GetProperty(i);
            }
    
            WritePacket(packet);
        }
    }
    Heres the constructor for ByteStream

    Code:
    template <class T>
    ByteStream::ByteStream(const T& data)
    {
        buffer = 0;
        Clear(sizeof(T));
        Write((char*)&data, sizeof(T));
    }
    The compiler is mingw

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This
    Code:
    ByteStream packet(PacketInt1(UNLIMITEDSIG));
    is actually passing a function (constructor?) object to the ByteStream constructor. At least, that is my best guess from the last four error messages.

    I'm not sure why this is, or how you can fix it, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    PacketInt1 is a struct with constructors. I thought doing "Blah blah(32)" and "Blah blah = 32;" would do the same thing. Could it be a bug with the compiler?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears that this construct is ambiguous and is therefore interpreted as a function pointer declaration. (The compiler is correct - it is more a problem with the language that a coder can't immediately see what a line of code might mean.)

    I seem to remember that this problem can be solved by adding an extra pair of brackets where they would be illegal, were this a function pointer.

    Code:
    ByteStream packet((PacketInt1(UNLIMITEDSIG)));
    Or alternatively give a name to the temporary:
    Code:
    PacketInt1 p(UNLIMITEDSIG);
    ByteStream packet(p);
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Strange Bug?
    By okinrus in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2004, 01:00 PM
  3. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  4. Very Strange Bug
    By Asm_Freak in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2003, 11:04 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM