Where is the Employee.cpp file located in the code?

  • Context: C/C++ 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Error Mac
Click For Summary

Discussion Overview

The discussion revolves around the issue of a missing implementation file for a C++ class, specifically the "Employee.cpp" file, which is necessary for defining methods declared in the "Employee.h" header file. Participants are addressing a coding problem related to class method implementations and error messages encountered during compilation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes the absence of implementations for the getter and setter functions in the "Employee" class, suggesting that this is the cause of the undefined symbol errors.
  • Another participant emphasizes the importance of providing method definitions and points out that the header file only contains declarations.
  • A participant expresses frustration with the use of screenshots for error messages, advocating for posting text instead to facilitate better understanding.
  • There is a suggestion that the original poster (OP) should reflect on the error messages to gain insight into the problem.
  • The OP acknowledges forgetting to create the "Employee.cpp" file and expresses gratitude for the assistance received.
  • Another participant confirms that they also initially wondered about the missing "Employee.cpp" file upon reviewing the code.

Areas of Agreement / Disagreement

Participants generally agree that the lack of a corresponding implementation file is the primary issue, but there is some debate regarding the best practices for sharing error messages and the OP's approach to debugging.

Contextual Notes

There is an implied assumption that participants are familiar with C++ programming and the structure of class definitions, which may not be clear to all readers. The discussion does not resolve the broader implications of coding practices or the learning process involved.

member 428835
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!
 
Technology news on Phys.org
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.
 
jedishrfu said:
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.

jedishrfu said:
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   Reactions: Vanadium 50
Mark44 said:
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.
 
He could simply copy and paste the error messages no need for a screenshot or retyping the message.
 
jedishrfu said:
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   Reactions: Mark44 and jedishrfu
From post #3:
Mark44 said:
In future posts, @joshmccraney, please copy the text of the error messages and post it here, rather than a screenshot.

Post #5:
jedishrfu said:
He could simply copy and paste the error messages no need for a screenshot or retyping the message.
 
  • Like
Likes   Reactions: jedishrfu
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   Reactions: berkeman, jedishrfu, jtbell and 1 other person
Yeah, that was my first thought when I saw the code in the opening post: "Where's Employee.cpp?" :confused:
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K