Linking error Clion mac

  • #1
joshmccraney
Gold Member
2,253
143
I have the following two files stored in the folder "impl" on my Desktop, the CLion IDE and am running on a mac: main.cpp:
C++:
#include <iostream>
#include "Employee.h"

int main() {

Employee e1("Bradly", "E007");
    Employee e2("John", "E425");

    std::cout << e1.getName() << ": " << e1.getId() << "\n";
    e1.setName("Brad");
    std::cout << e1.getName() << ": " << e1.getId() << "\n";
    std::cout << e2.getName() << ": " << e2.getId() << "\n";

    return 0;
}

and then a header file Employee.h

C++:
#pragma once
#include <string>

class Employee
{
public:
Employee(std::string name, std::string id);

    std::string getName() const;
    std::string getId() const;

    void setName(std::string name);

private:
std::string m_name;
    std::string m_id;
};

When clicking the green triangle "run" (see the arrow in the image) I get the error the image shows (see the file structure upper left if this helps):
Screenshot 2023-02-07 at 11.24.16 PM.png

Any idea what I'm doing wrong? Thanks so much!
 

Answers and Replies

  • #2
14,291
8,320
Aren't you supposed to provide implementations for the getter and setter functions in your employee class definition?

Something along the lines in this example:

https://cplusplus.com/forum/beginner/254961/

where you can see implementation code for the getters and setters. The getters return the private attribute like m_name or m_id and the setter updates the m_name attribute.

Your errors as best as I can read the tiny print in your screencap indicate that the getter and setter methods are undefined symbols.
 
  • #3
36,877
8,924
Your errors as best as I can read the tiny print in your screencap indicate that the getter and setter methods are undefined symbols.
Your eyes are better than mine. Screen shots of error messages often turn out to be completely unreadable, in my experience. Even worse is with white letters against a black background.

In future posts, @joshmccraney, please copy the text of the error messages and post it here, rather than a screenshot.

Aren't you supposed to provide implementations for the getter and setter functions in your employee class definition?
This code is a very simple example. It should be obvious that the header file contains only declarations of these two class methods, with no definitions for them. Of course, the linker will complain.
 
  • Like
Likes Vanadium 50
  • #4
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
29,947
15,635
Screen shots of error messages often turn out to be completely unreadable,
I do so enjoy the gray-on-gray color scheme.

If the OP had to type in the error message, it might have caused him to think about what it meant and pointed him in the right direction. We have at least two members here who post "PF! Debug my code for me!" messaged and I see no signs of progress from either.

OP, if you thought this code could ever possibly have worked, you are missing a very important thing about classes and methods. (Maybe even programming in general) I would jump back several chapters and try and fill the gaps before moving on.
 
  • #5
14,291
8,320
He could simply copy and paste the error messages no need for a screenshot or retyping the message.
 
  • #6
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
29,947
15,635
He could simply copy and paste the error messages
This is true. But I hope writing them himself might trigger some thinking.
 
  • Like
  • Haha
Likes Mark44 and jedishrfu
  • #7
36,877
8,924
From post #3:
In future posts, @joshmccraney, please copy the text of the error messages and post it here, rather than a screenshot.

Post #5:
He could simply copy and paste the error messages no need for a screenshot or retyping the message.
 
  • #8
joshmccraney
Gold Member
2,253
143
Wow, I definitely forgot about writing a .cpp file. Feel like a moron looking back at this now, but thanks for the help! And in the future I'll post the error in text rather than a screenshot. Thanks so much to you all!
 
  • Like
Likes berkeman, jedishrfu, jtbell and 1 other person
  • #9
jtbell
Mentor
15,971
4,775
Yeah, that was my first thought when I saw the code in the opening post: "Where's Employee.cpp?" :confused:
 

Suggested for: Linking error Clion mac

Replies
12
Views
303
Replies
1
Views
388
  • Last Post
Replies
23
Views
2K
Replies
32
Views
936
  • Last Post
Replies
14
Views
577
Replies
3
Views
762
Replies
18
Views
451
Replies
2
Views
525
  • Last Post
Replies
2
Views
992
  • Last Post
3
Replies
70
Views
2K
Top