0

I'm getting both of following errors: "undefined reference to `Vessel::Vessel()' " and: "Id returned 1 exit status" but nothing shows up. How do I fix it? I tried everything but I can't come up with a solution.

It can be seen that the problem is in merging in classes or references but I can't figure out what exactly

Here is the code of the whole program:

#include<iostream>
#include<string>
using namespace std;

class Vessel{

 protected:

   string registration; 

   int power;

 public:

   Vessel();

   Vessel(string r, int power){
    r=registration;
    power=power;
   };
    
    string set_registration(string put){
        registration=put;
    }
    
   string get_registration(){
    return registration;
   }
    double set_power(double set){
        power=set;
    }
   double get_power(){
        return power;
   }

   virtual void print()=0;

};

class Speedboat : public Vessel{
    private:
        int speed;
    public:
        set_speed(int s){
            speed=s;
        }
        get_speed(){
            return speed;
        }
        void print(){
            cout<<get_registration()<<" "<<get_power()<<" "<<get_speed();
        }
};
class Ferry : public Vessel{
    private:
        int capacity;
};
int main(){
    Vessel * ptr;
    Speedboat obj1;
    ptr=&obj1;
    obj1.set_power(5.2);
    obj1.set_registration("ZG5212");
    ptr->print();
}
Jan Tuđan
  • 233
  • 3
  • 17

2 Answers2

1

You haven't implemented Vessel:: Vessel(), only declared it. One possible implementation would be to delegate the default constructor to the constructor that actually initializes the member variables:

Vessel() : Vessel("", 0.0) {}   // delegates to the below

Vessel(string r, int p) : registration(std::move(r)), power(p) {}

This way you can access the values of a default constructed Vessel without causing undefined behavior which would be the case if you use the default implementation: Vessel() = default;.

Also:

  • set_speed() and get_speed() must have a type (or be declared void).
  • set_registration() is declared to return a string but doesn't return anything.
  • set_power() is declared to return a double but doesn't return anything.

It seems like the setter functions are supposed to return the old value. For this you can use the std::exchange function from the <utility> header. Example:

double set_power(double set){
    return std::exchange(power, set); // return the old power and set the new
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • How do I exactly implement it? – Jan Tuđan Sep 13 '20 at 21:33
  • @JanTuđan It seems you figured it out from the other answer? – Ted Lyngmo Sep 14 '20 at 05:19
  • Yes I did, in my way of coding constructor should have been on the default, but this answer was also helpful. – Jan Tuđan Sep 14 '20 at 09:59
  • @JanTuđan Great. I made some additions to the answer that may be useful too. – Ted Lyngmo Sep 14 '20 at 10:15
  • This declaration seems better instead of setting the constructor to the default. Cool, I included utility header, but it says that exchange is not a member of 'std', do you know what's wrong? Is maybe the problem if I have std namespace included? – Jan Tuđan Sep 14 '20 at 11:39
  • @JanTuđan `std::exchange` requires c++14 so if you are using C++11 it's not available. It doesn't do anything magic though and it's easy to create your own version of it to use with C++11. Just look at the _possible implementation_ @ [std::exchange](https://en.cppreference.com/w/cpp/utility/exchange). – Ted Lyngmo Sep 14 '20 at 11:50
  • I'm using c++03 version, maybe it supports c++11 but it's definitely not c++14 version. Okay, this helps me too. – Jan Tuđan Sep 14 '20 at 13:37
  • @JanTuđan Oh, c++03?! That's really old. Any reason why you stick with that version? – Ted Lyngmo Sep 14 '20 at 13:53
  • I have no idea, I'm using Dev-c++ 5.11 version (TDM-GCC 4.9.2), but I don't know which c++ version it supports, I supposed it supports c++03, maybe c++11? I don't really know. – Jan Tuđan Sep 14 '20 at 14:05
  • I would definitely upgrade. C++11 comes with a lot of great features. 14 and 17 too, but 11 is a real boost compared to the older versions. – Ted Lyngmo Sep 14 '20 at 14:10
  • 1
    I'd love to try c++11 version and compare it to this one (which I'm using right now). – Jan Tuđan Sep 14 '20 at 14:21
1

The problem is here:

class Vessel{

...   

 public:

   Vessel();

You'll need to provide a definition for the default ctor, for example Vessel() = default;

Note, you have a parameterized ctor for Vessel that's not utilized, at least not in your code snippet. Did you mean for that to be there? If so, did you mean to utilize it somehow? The way the code is written, you could just delete that.

sfb103
  • 264
  • 1
  • 7