something is wrong with references of base class and derived class.

The problem is seems to be the referencing of the base class and the derived class, and when I initialize the constructor and the variables in cpp file it seem to be the problem I think. However I had look at it very carefully and cannot seem to find the actual problem. Not really familiarize with initializing constructors in .cpp file, been following the book. Thank you in advance for the help.

The error Xcode is giving is --- Undefined symbols for architecture x86_64:
"vehicle::vehicle(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int)", referenced from:
truck::truck(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, int&, double, int) in person.o
"vehicle::vehicle()", referenced from:
truck::truck() in person.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) ---

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
//person.hpp file, header file.

#include <string>
#include <iostream>

class vehicle{
public:
    vehicle();
    vehicle(string& theName, int numOfCylinder);
    string getName() ;
    int numOfCylinder() ;
    void setName(string makeName, int numOfCylinder);
    
    
private:
    string name;
    int cylinder;
};


class truck: public vehicle{
public:
    truck();
    truck( string & theName, int & numOfCylinder,
          double theTons, int thePounds);
    void setTons(double newTons);
    void setPounds(int pounds);
    double getWeight();
    
    
private:
    double tons;
    int pounds;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//.cpp source file.

#include "person.hpp"
#include <string>
#include <iostream>
using std :: string;

truck :: truck(): vehicle(), tons(0), pounds(0){

//CONSTRUCTOR..
}
truck ::  truck (string& theName, int & numOfCylinder, double theTons, int thePounds) : vehicle(theName,numOfCylinder), tons(theTons), pounds(thePounds){


}
As far as I know, you can’t use delegating constructor and member initializer list together: you need to choose between them:
If the name of the class itself appears as class-or-identifier in the member initializer list, then the list must consist of that one member initializer only

http://en.cppreference.com/w/cpp/language/initializer_list
You can’t use delegating constructor and member initializer list together
This isn't a delegating constructor -- that's a special case of T::T() delegating it's work to another T::T(). It is perfectly legal to call a base-class constructor in a member-initializer list in a non-delegating constructor.

This is a linker error. vehicle::vehicle() is not defined. If it does nothing, just default it in the header file (line 8) vehicle() = default;
Last edited on
You are right, @mbozzi, I’ve mixed inheriting constructors and delegating constructors. Thank you for your correction.

Sorry for the wrong advice, @shiHear.
Thank you guys for your answer, the problem seem to be not initializing vehicle constructor in the .cpp file. And when I did that, the errors disappear. However, in the book that I am using did not initialize
vehicle:: vehicle constructor, it only uses the derived class truck to call to vehicle using the : . Thank you, for your help.
1
2
3
4
5
vehicle::vehicle(string& theName, int numOfCylinder){



}
Topic archived. No new replies allowed.