0

I have problem with an assignment. Im trying to read from a file where each written line corresponds to an object (ShapePtr) and am supposed to initialize an object from each line and store them in a list. All ShapePtr have a pointer to a shape.

here is the stub from the main method that is complaining:

ifstream is("test.dat");
istream_iterator<ShapePtr> shapein(is), endofshapein;
list<ShapePtr> shapelist(shapein, endofshapein);

for (list<ShapePtr>::iterator it = shapelist.begin(); it != shapelist.end(); ++it)
     cout << *it << endl;

The definition of ShapePtr:

class ShapePtr
{
    private:
        Shape *shape;
    public:
        ShapePtr() { shape = 0; }
        ShapePtr(const ShapePtr& shptr);
        ShapePtr& operator=(const ShapePtr& shptr);
        ShapePtr(Shape *ptr) { shape = ptr; }
        ~ShapePtr() { delete shape; }
        ShapePtr* clone() const;
        friend ostream& operator<<(ostream& out, const ShapePtr& sh);
        friend istream& operator>>(istream& in, ShapePtr& sh);
};

inline ostream& operator<<(ostream& out, const ShapePtr& sh)
{
    sh.shape->print(out);
    return out;
}

inline istream& operator>>(istream& in, ShapePtr& sh)
{
    string name, data;
    in >> name;
    getline(in, data);
    cout << name << '\n';
    cout << data << endl;
    //sh.shape->read(in);   // Not currently implemented
    sh = ShapePtr(new Point(1.0, 2.0, 3.0)); // Just for testing 
    return in;
}

The implementation of ShapePtr:

ShapePtr::ShapePtr(const ShapePtr& shptr)
{
    if(shptr.shape)
        shape = shptr.shape->clone();
    else
        shape = 0;
}

ShapePtr& ShapePtr::operator=(const ShapePtr& shptr)
{
    if(shptr.shape)
        shape = shptr.shape->clone();
    else
        shape = 0;
    return *this;
}

ShapePtr *ShapePtr::clone() const
{
   return new ShapePtr(*this);
}

Im currently just trying to see what gets read from the file so I can move on to implementing the initialization of the shapes. But right now I get the following error when I try do run:

Undefined symbols for architecture x86_64:
"ShapePtr::ShapePtr(ShapePtr const&)", referenced from:
std::__1::list<ShapePtr, std::__1::allocator<ShapePtr> >::push_back(ShapePtr const&) in main-1a9984.o 
std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>::istream_iterator(std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> const&) in main-1a9984.o "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, ShapePtr const&)", referenced from:
_main in main-1a9984.o "operator>>(std::__1::basic_istream<char, std::__1::char_traits<char> >&, ShapePtr&)", referenced from:
_main in main-1a9984.o std::__1::list<ShapePtr, std::__1::allocator<ShapePtr> >::list<std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> >(std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>, std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long>, std::__1::enable_if<__is_input_iterator<std::__1::istream_iterator<ShapePtr, char, std::__1::char_traits<char>, long> >::value, void>::type*) in main-1a9984.o
ld: symbol(s) not found for architecture x86_64

What am I doing wrong? I have tried to fix this myself but Im getting nowhere. Thanks

Edit:

What my problem was: not including the file shapeptr.cpp in the compile command.

patriques
  • 5,057
  • 8
  • 38
  • 48
  • The error message is pretty clear, you have to define the copy constructor: `ShapePtr::ShapePtr(ShapePtr const&)`. You seem to have missed it. – πάντα ῥεῖ Jun 10 '14 at 16:57
  • @πάνταῥεῖ I edited the question. Is it still a duplicate? – patriques Jun 10 '14 at 18:23
  • Might still be one of the reasons listed there (it's pretty complete). Can you show your linker command line? (could be e.g. a problem of object file order). – πάντα ῥεῖ Jun 10 '14 at 18:26
  • Thanks, Now I see my problem was really silly. Not including .cpp file. Well this question maybe should be closed I don´t know. – patriques Jun 10 '14 at 18:31
  • I think that's also [one of the answers](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400) in the marked dupe. Unfortunately you can't delete your question directly, because there's an answer (which is not addressing your real problem). – πάντα ῥεῖ Jun 10 '14 at 18:34

1 Answers1

2

You declared ShapePtr's copy constructor but didn't implement it. Either implement it, or remove the prototype to use the default one.

Since you defined ShapePtr::~ShapePtr(), you should implement it together with ShapePtr::operator=() to get expected behavior.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
timrau
  • 22,578
  • 4
  • 51
  • 64
  • 1
    I edited the question. I actually had implemented the assign operator and copycosntructor, I just did not include them. Do you still have an idea of why this is still? – patriques Jun 10 '14 at 18:25