0

I'm relatively new to CPP, currently in my second class for it, and while I was trying to compile a lab for my class I keep getting this error. I thought it might have something to do with file path but that doesn't seem to the be the case, does anyone have any suggestions? Here is the code:

#include <iostream>
#include <string>

using namespace std;

class personType {
    private:
        string firstName;
        string lastName;
    
    public:
        void setName (string, string) {
            cin >> firstName;
            cin >> lastName;
        }

        const string getFirstName() {
            return firstName;
        }

        const string getLastName() {
           return lastName;
        }
        
        void print() {
            cout << firstName << ", " << lastName << endl;
        }
        personType(string = "", string = "");
};

int main() {
    personType John;


    John.setName("John", "Doe");
    John.getFirstName();
    John.getLastName();
    John.print();

    return 0;
}

Here is the compiler error message:

Undefined symbols for architecture x86_64: "personType::personType(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >)", referenced from: _main in CS200Lab1-022e17.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Build finished with error(s).

kareemta
  • 3
  • 1

3 Answers3

0

The problem is your class has a method name with no code.

personType(string = "", string = "");

The error message is simply telling you there is a declaration for personType::personType(string,string) but it can't find any definition code for this method. You can remove this line, as it is not necessary, and the compiler will add an implicit constructor with no parameters automatically.

Alternatively you can add a body of code for this constructor:

personType(string first = "", string last = "")
{
// code here
}

Maybe your intention is something like this:

#include <iostream>
#include <string>

using namespace std;

class personType {
private:
    string _firstName;
    string _lastName;

public:
    void setName(string firstName, string lastName) {
        _firstName = firstName;
        _lastName = lastName;
    }

    const string getFirstName() {
        return _firstName;
    }

    const string getLastName() {
        return _lastName;
    }

    void print() {
        cout << _firstName << ", " << _lastName << endl;
    }
   
    personType(string firstName = "", string lastName = "")
    {
        _firstName = firstName;
        _lastName = lastName;
     }
};

int main() {
    personType John;

    John.setName("John", "Doe");
    John.getFirstName();
    John.getLastName();
    John.print();

    return 0;
}
Malcolm McCaffery
  • 2,468
  • 1
  • 22
  • 43
  • Yes, this was my first experience with constructors and I didn't realize that they needed a body. Thank you so much your comment really helped me out! – kareemta Jan 24 '22 at 00:07
0

This is only a declaration (without definition) which is why the linker complains:

personType(string = "", string = "");

That is, you've promised that there will be a definition somewhere when the linking starts, but since there is none, linking fails.

This would be a definition (and declaration):

personType(string first = "", string last = "") 
    : // colon starts the member initializer list in constructors
    firstName(first), 
    lastName(last) 
{
    // the function body can be empty
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-1

There was an issue with the compiler because the constructor function was missing a body.

kareemta
  • 3
  • 1