0

I am building a very small application to help me learn C++ and its quirks, and I have run into a wall with my development. I keep getting this error and don't know how to solve it. In the code below, I am trying to create an instance of the user class in the main function (not shown). I don't understand exactly where I am going wrong; any help anyone can provide is appreciated.

Undefined symbols for architecture x86_64
  "User::User()", referenced from:
      _main in main-e8c60e.o
      _main in main-dac910.o

Here is my header file

#include <string>
#include <iostream>
class User
{
private:
    std::string firstName_;
    std::string lastName_;
    std::string favGenre_;
    int userAge_;

public:
    /**
     * @brief Construct a new User object
     *
     * The user class creates a new user object with all users' information.
     * On initialization, it will create the instance of the BookLocker class
     * and be on standby as the User uses the OpenLibary class.
     *
     */
    User();

    // Set name for current user
    void setName();

    // Set favorite genre for user
    void setGenre();

    // Set the users age
    void setAge();

    // Get users name
    std::string getName();

    // Get the users favorite genre
    std::string getGenre();

    // Get the users age
    int getAge();
};

Here is the implementation

/**
 * A class that represents the User gathering their information and creating
 * an instance of the book locker alongside its creation.
 *
 */
#include <iostream>
#include <string>
#include "../include/User.h"

/// Initialize the user object

User::User()
{
    std::cout << "What is your first name?" << std::endl;
    std::cin >> firstName_;

    std::cout << "What is your last name? " << std::endl;
    std::cin >> lastName_;

    std::cout << "What is your favorite literature genre? " << std::endl;
    std::cin >> favGenre_;

    std::cout << "How old are you? " << std::endl;
    std::cin >> userAge_;
}

void User::setName()
{
    std::cout << "What is your first name?" << std::endl;
    std::cin >> firstName_;
    std::cout << std::endl;
    std::cout << "What is your last name?" << std::endl;
    std::cin >> lastName_;
}

void User::setAge()
{
    std::cout << "What is your age?" << std::endl;
    std::cin >> userAge_;
}

void User::setGenre()
{
    std::cout << "What is your favorite genre?" << std::endl;
    std::cin >> favGenre_;
}

int User::getAge()
{
    return userAge_;
}

std::string User::getName()
{
    return firstName_ + " " + lastName_;
}

std::string User::getGenre()
{
    return favGenre_;
}

Any help on this would be greatly appreciated. Thank you.

Kevin H
  • 127
  • 1
  • 7
  • 6
    I'm fairly certain that you are not compiling and linking your implementation file, hence the linker error – UnholySheep Jan 13 '22 at 13:22
  • 3
    if you are using VSCode and not using any extensions the default behavior builds only the 1 active file into your executable. All other .cpp files in the folder are ignored. The official documentation tells you what change to make to your tasks.json to build all the files in your folder here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jan 13 '22 at 13:35
  • 2
    The error is related to the way you are compiling your code. Do you use an IDE (and which one), or are you compiling from command line? – Serge Ballesta Jan 13 '22 at 13:36
  • 2
    Since we can see you have implementation of `User::User()` your problem is caused by invalid build process. How do you build your program? This is scary `#include "../include/User.h"` and shows you have problem understanding build process. – Marek R Jan 13 '22 at 13:56

0 Answers0