Undefined reference to main - which is impossible.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

55 Replies - 1799 Views - Last Post: 21 October 2013 - 09:39 PM Rate Topic: -----

#1 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:05 PM

It won't compile my code and is acting weird.

work.cpp (this is my MAIN.cpp file) I just renamed it work

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <functional>
#include <map>

#include "Entity.h"
#include "Armor.h"
#include "Weapon.h"
#include "Item.h"
#include "Creature.h"
#include "XMLSerializable.h"

using namespace std;

XMLSerializable * constructItem()
{
        return new Item;
}


void  outputXML(vector<XMLSerializable*> & vObjects, ostream & output)
{


        output << "<?xml version=\"1.0\" encoding = \"utf-8\"?>"
                << endl
                << "<World>"
                << endl;

        for( auto it = vObjects.begin();
           it != vObjects.end();
           it++ )
        {
          (*it)->writeFragment(output);
        }

      output << "</World>" << endl;
}

void dumpObjects(vector<XMLSerializable*> & vObjects)
{
    for( auto it = vObjects.begin();
        it != vObjects.end();
        it++)
{
        (*it)->dumpObject();
        cout << endl;
}

}

void parseElement(istream & input, string sPrefix)
{
        string sElementName;
        getline(input, sElementName, '>');

        if( sElementName.back() == '/' )
        {
                cout << sPrefix << "Empty element: " << sElementName << endl;
                return;
        }
        else
        {
                cout << sPrefix << "Element - " << sElementName << endl;
        }

        string sContent = "";


        while( true )
        {

                char c = input.get();
 while( c != '<' )
                {
                        if( c != '\n' )
                                sContent.push_back(c);

                        c = input.get();
                }

                if( input.peek() == '/' )
                {
                        input.get();
                        string sEndElement;
                        getline(input, sEndElement, '>');

                        if( sEndElement != sElementName )
                                cout << "Bad XML found" << endl;


                        cout << sPrefix << "Content - " << sContent << endl;

                        return;
                }
                else
                {
                        parseElement(input, sPrefix + "  ");
                }

        }

}

void parseXML(string sFilename)
{
        ifstream input;
        input.open(sFilename);
 while( input.get() != '?' );
        while( input.get() != '?' );

        if( input.get() != '>' )
        {
                cout << "Bad XML detected" << endl;
                return;
        }

        while( input.get() != '<' );


        parseElement(input, "");

}

int main(int argc, char * argv[])
{




        map<string, function<XMLSerializable*()>> mapConstructor;

        mapConstructor["Item"] = []() {return new Item; };

        mapConstructor["Weapon"] = []() { return new Weapon; };
        mapConstructor["Armor"] = []() { return new Armor; };
        mapConstructor["Creature"] = []() { return new Creature; };

        string sLookup;
        cout << "What class do you want to look up? " << endl;
        cin >> sLookup;

        function<XMLSerializable*()> pFunc = mapConstructor[sLookup];

        if(pFunc == NULL)
        {
          cout << "Class" << sLookup << " not found. " << endl;
        }
        else
        {
          XMLSerializable * pObject = pFunc();

                if (pObject == NULL)
                {
                        cout << "Couldn't construct object. " << endl;
                }
                else
                {
        cout << "Object constructed." << endl;
                }

        }


        cout << "What file should we parse? ";
        string sFilename;
        cin >> sFilename;

        parseXML(sFilename);



        return 0;
}


g++ --std=c++0x work.cpp

Errors:
/tmp/ccxzuGmp.o: In function `constructItem()':
work.cpp:(.text+0x18): undefined reference to `Item::Item()'
/tmp/ccxzuGmp.o: In function `main::{lambda()#1}::operator()() const':
work.cpp:(.text+0x610): undefined reference to `Item::Item()'
/tmp/ccxzuGmp.o: In function `main::{lambda()#2}::operator()() const':
work.cpp:(.text+0x656): undefined reference to `Weapon::Weapon()'
/tmp/ccxzuGmp.o: In function `main::{lambda()#3}::operator()() const':
work.cpp:(.text+0x69c): undefined reference to `Armor::Armor()'
/tmp/ccxzuGmp.o: In function `main::{lambda()#4}::operator()() const':
work.cpp:(.text+0x6e2): undefined reference to `Creature::Creature()


What does this mean?

This post has been edited by shamieh: 16 October 2013 - 08:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined reference to main - which is impossible.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,079
  • Joined: 05-May 12

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:13 PM

Is that your entire compile line? Were are you linking in the implementation of Item, Weapon, Armor, and Creature classes?
Was This Post Helpful? 1
  • +
  • -

#3 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:16 PM

Item.h
#ifndef _Item_included_
#define _Item_included_
#include <ostream>
#include "Entity.h"

class Item : public Entity
{
public:
        Item();
        int getWeight();
        void setWeight(int iWeight);
        int getValue();
        void setValue(int iValue);
        //NEW
        virtual void dumpObjectData();
        virtual void dumpObject();
        virtual void writeDataAsFragment(std::ostream & output);
        virtual void writeFragment(std::ostream & output);

private:
        int m_iWeight;
        int m_iValue;
};

#endif


Item.cpp
#include "Item.h"

Item::Item()
{
        m_iWeight = 0;
        m_iValue = 0;
}

void Item::setWeight(int iWeight)
{
        m_iWeight = iWeight;
}

int Item::getWeight()
{
        return m_iWeight;
}

void Item::setValue(int iValue)
{
        m_iValue = iValue;
}


int Item::getValue()
{
        return m_iValue;
}

void Item::dumpObjectData()
{
        Entity::dumpObjectData();

        std::cout << "   Weight  : " << getWeight() << std::endl;
        std::cout << "    Value  : " << getValue() << std::endl;
}

void Item::dumpObject()

{
        std::cout << "Item: " << std::endl;

        dumpObjectData();
}

void Item::writeDataAsFragment(std::ostream & output)
{
        Entity::writeDataAsFragment(output);

        output << " <weight> "
        << getWeight()
        << "   </weight> "
        << std::endl
        <<"    </value>"
        << getValue()
        << " </value>"
        << std::endl;
}

void Item::writeFragment(std::ostream & output)
{
        output << " <Item> " << std::endl;

        writeDataAsFragment(output);

        output << " </Item> " << std::endl;
}


View PostSkydiver, on 16 October 2013 - 08:13 PM, said:

Is that your entire compile line? Were are you linking in the implementation of Item, Weapon, Armor, and Creature classes?


Just posted the Item .h & .cpp. All of them are inheriting/deriving from a XMLSerializable base class .h file. Shall I post that too?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,079
  • Joined: 05-May 12

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:16 PM

Yes, that is the source code for Item.h and Item.cpp, but let me repeat my question. Where are you linking in the compiled version of Item?

Can you post your entire makefile or build script?
Was This Post Helpful? 1
  • +
  • -

#5 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:24 PM

Here is the makefile our teacher gave us, I'm not sure if I need to edit it or what.

I'm assuming this is what is linking them?

makefile

OBJECTS = Armor.o Character.o Consumable.o Creature.o Entity.o Equipment.o Item.o Player.o Weapon.o XMLSerializable.o

HEADERS = Armor.h Character.h Consumable.h Creature.h Entity.h Equipment.h Item.h Player.h Weapon.h XMLSerializable.h





prog1: $(OBJECTS) main.o
        g++ $^ -o prog1

la5: $(OBJECTS) la5.o
        g++ $^ -o la5

%.o: %.cpp $(HEADERS)
        g++ --std=c++0x -c  $< -o $@

clean:
        rm -f *.o prog1 la5

This post has been edited by shamieh: 16 October 2013 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,079
  • Joined: 05-May 12

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 08:56 PM

So you should be building stuff by running 'make', not by running 'g++ --std=c++0x work.cpp'.
Was This Post Helpful? 1
  • +
  • -

#7 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:11 PM

I guess, It doesn't really matter though. Make is essentially just a macro to compile all my programs correct? I just ran make and it looks like I got almost the same errors?

My Errors after running make. NOTE: I Renamed work.cpp back to main.cpp for consistency.

undefined reference to `Armor::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Armor.o:(.rodata._ZTV9Equipment[vtable for Equipment]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Character.o:(.rodata._ZTV9Character[vtable for Character]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Consumable.o:(.rodata._ZTV10Consumable[vtable for Consumable]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Creature.o:(.rodata._ZTV8Creature[vtable for Creature]+0x10): undefined reference to `Creature::dumpObject()'
Creature.o:(.rodata._ZTV8Creature[vtable for Creature]+0x30): undefined reference to `Creature::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Entity.o:(.rodata._ZTV6Entity[vtable for Entity]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Item.o:(.rodata._ZTV4Item[vtable for Item]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Player.o:(.rodata._ZTV6Player[vtable for Player]+0x30): undefined reference to `Entity::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Weapon.o:(.rodata._ZTV6Weapon[vtable for Weapon]+0x30): undefined reference to `Weapon::setElementData(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

Was This Post Helpful? 0
  • +
  • -

#8 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:16 PM

Which, by the way I have no idea what these errors mean.
Was This Post Helpful? 0
  • +
  • -

#9 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:19 PM

Those are linker errors that occur when you reference code that cannot be found.

Zip up all the code and the makefile and attach it to a post.
Was This Post Helpful? 0
  • +
  • -

#10 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:26 PM

View Posteker676, on 16 October 2013 - 09:19 PM, said:

Those are linker errors that occur when you reference code that cannot be found.

Zip up all the code and the makefile and attach it to a post.


All the code in the makefile has been posted, it is like 4 lines. Here it is again though if this will help. Give me a few minutes I will post all the rest of the code.

OBJECTS = Armor.o Character.o Consumable.o Creature.o Entity.o Equipment.o Item.o Player.o Weapon.o XMLSerializable.o

HEADERS = Armor.h Character.h Consumable.h Creature.h Entity.h Equipment.h Item.h Player.h Weapon.h XMLSerializable.h





prog1: $(OBJECTS) main.o
        g++ $^ -o prog1

la5: $(OBJECTS) la5.o
        g++ $^ -o la5

%.o: %.cpp $(HEADERS)
        g++ --std=c++0x -c  $< -o $@

clean:
        rm -f *.o prog1 la5


This post has been edited by shamieh: 16 October 2013 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:28 PM

He meant all your program files, the source and include files, along with the make file.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:44 PM

How do I upload multiple files...Every time I click the "Try our advanced uploader for multiple files" it takes me to the USER CP...
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:48 PM

That's why he recommended zipping all your files into one zip file.

Jim
Was This Post Helpful? 2
  • +
  • -

#14 shamieh  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 146
  • Joined: 14-September 13

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:53 PM

Just figured out how to upload multiple code. I'm not permitted to upload any of it though, that is the error message I am receiving. For The .h files I am getting this message "You aren't permitted to upload this kind of file"
Was This Post Helpful? 0
  • +
  • -

#15 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Undefined reference to main - which is impossible.

Posted 16 October 2013 - 09:55 PM

Zip up the files (or tar them or rar them) and upload a single .zip/.tar.gz/.rar/etc.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »