Won't compile.

This .cpp Won't compile. It's acting like it doesn't have a main reference. (i.e. a main()) but it does, in the main.cpp file. In my main.cpp file I have #include "XMLSerializable.h" - which is correlated with XMLSerializable.cpp. So It should recognize that it has a main func because i have int main() right? What am I doing wrong?

g++ --std=c++0x XMLSerializable.cpp
errors:/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'


my code..

XMLSerializable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "XMLSerializable.h"

using namespace std;

void XMLSerializable::dumpObject()
{
}

void XMLSerializable::dumpObjectData()
{
}

void XMLSerializable::writeFragment(ostream & output)
{
}

void XMLSerializable::writeDataAsFragment(ostream & output)
{
}

void XMLSerializable::setElementData(string sElementName, string sValue)
{
}


XMLSerializable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _XMLSerializable_included_
#define _XMLSerializable_included_

#include <iostream>
#include <ostream>
#include <string>

class XMLSerializable
{
public:
        virtual void dumpObject();
        virtual void dumpObjectData();
        virtual void writeFragment(std::ostream & output);
        virtual void writeDataAsFragment(std::ostream & output);
        virtual void setElementData(std::string sElementName, std::string sValue);
};


#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
main.cpp
#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"] = constructItem();
  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;
}


here are errors I get when I try to compile main.cpp

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

_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = XMLSerializable*, _Res = XMLSerializable*, _ArgTypes = {}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>&>::type = std::function<XMLSerializable*()>&]â
main.cpp:138:48: instantiated from here
/usr/include/c++/4.6/functional:1764:40: error: â* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer [with _Functor = XMLSerializable*]((* & __functor))â cannot be used as a function


I got it to compile. Now I'm getting this error:

/tmp/cc4kty4E.o: In function `constructItem()':
work.cpp:(.text+0x18): undefined reference to `Item::Item()'
/tmp/cc4kty4E.o: In function `main::{lambda()#1}::operator()() const':
work.cpp:(.text+0x610): undefined reference to `Item::Item()'
/tmp/cc4kty4E.o: In function `main::{lambda()#2}::operator()() const':
work.cpp:(.text+0x656): undefined reference to `Weapon::Weapon()'
/tmp/cc4kty4E.o: In function `main::{lambda()#3}::operator()() const':
work.cpp:(.text+0x69c): undefined reference to `Armor::Armor()'
/tmp/cc4kty4E.o: In function `main::{lambda()#4}::operator()() const':
work.cpp:(.text+0x6e2): undefined reference to `Creature::Creature()'
<nolyc> Undefined reference is a linker error.
It's not a compile error. #includes don't help.
You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line.
Check which one. (Note that some linkers call it an unresolved external)


> So It should recognize that it has a main func because i have int main() right? What am I doing wrong?
You never say that you want to use main.cpp
$ g++ -std=c++11 main.cpp XMLSerializable.cpp -o program.bin
If you find it convenient you can separate the compilation and link stages.

Also, you are probable leaking memory
Topic archived. No new replies allowed.