horHAY
horHAY

Reputation: 788

Qt: Undefined reference to

Just started using Qt and come across an error, wonder if someone could shed some light on the issue. Googled about and looked at similar questions but cant seem to get a solution;

C:\Users\Seb\Desktop\SDIcw2\main.cpp:10: error: undefined reference to `SDI::shipHandler::shipHandler(SDI::shipHandler&)'

occurs at line 10, the " w.populateCombo(shipHandler);" in my main.cpp;

#include "widget.h"
#include <QApplication>
#include "shipHandler.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    SDI::shipHandler shipHandler("ships/ships.txt");
    w.populateCombo(shipHandler);
    return a.exec();
}

shipHandler.cpp (constructor & destructor)

SDI::shipHandler::shipHandler(std::string fileName)
{
    shipCount = 0;
    std::string line;
    std::ifstream infile;
    infile.open(fileName.c_str());
    while(!infile.eof()) 
    {
        getline(infile,line); 
        shipHandler::lineParse(line);
        shipCount++;
    }
    infile.close();
}

SDI::shipHandler::~shipHandler()
{
}

shipHandler.h

#ifndef SDI__shipHandler
#define SDI__shipHandler
#include "common.h"
#include "navalVessels.h"

namespace SDI
{
    class shipHandler
    {
        //variables
    public:
        std::vector<SDI::navalVessels*> ships;
        int shipCount;
    private:

        //function
    public:
        shipHandler();
        shipHandler(std::string fileName);
        shipHandler(SDI::shipHandler& tbhCopied);
        ~shipHandler();

        void lineParse(std::string str);
        void construct(std::vector<std::string> line);
        std::vector<int> returnDates(std::string dates);
    private:
    };
}

#endif

Any help is appreciated

Upvotes: 0

Views: 909

Answers (1)

Brendan Long
Brendan Long

Reputation: 54232

Just reading the error message, it looks like it's trying to use the copy constructor (shipHandler(SDI::shipHandler& tbhCopied)), but you never fully defined it in shipHandler.cpp.

class shipHandler
{
    // ...
public:
    shipHandler(); // this isn't defined anywhere
    shipHandler(std::string fileName);
    shipHandler(SDI::shipHandler& tbhCopied); // this isn't defined anywhere
    ~shipHandler();

    // ...
};

First, you should either stop declaring the copy constructor, or finish defining it:

// in shipHandler.cpp
SDI::shipHandler::shipHandler(SDI::shipHandler& tbhCopied) {
    // fill this in
}

You should also either define or remove the default constructor (SDI::shipHandler::shipHandler()).

Next, you can probably pass your shipHandler as a reference instead of creating a copy:

// most likely, this is what you want
void Widget::populateCombo(const shipHandler& handler);

// or maybe this
void Widget::populateCombo(shipHandler& handler);

These may be useful references:

What is the difference between a definition and a declaration?

c++ passing arguments by reference and pointer

Upvotes: 2

Related Questions