英文:
Unable to resolve C2084 error: 'Function already has a body' - C++
问题
以下是翻译好的部分:
- Employee.h
#pragma once
#include <string>
class Employee {
public:
std::string name{};
int salary{};
Employee(std::string name, int salary);
virtual void displayDetails() {}
};
- Manager.h:
#pragma once
#include <string>
#include "Employee.h"
class Manager : public Employee {
private:
std::string department{};
public:
Manager(std::string name, int salary, std::string department);
void displayDetails() override {}
};
- Employee.cpp
#include <iostream>
#include "Employee.h"
#include <string>
Employee::Employee(std::string name, int salary) : name(name), salary(salary) {}
void Employee::displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Salary: " << salary << std::endl;
}
- Manager.cpp:
#include <iostream>
#include <string>
#include "Manager.h"
Manager::Manager(std::string name, int salary, std::string department) :
Employee(name, salary), department(department) {};
void Manager::displayDetails(){
std::cout << "Name: " << name << std::endl;
std::cout << "Salary: " << salary << std::endl;
std::cout << "Department: " << department << std::endl;
}
- Main.cpp:
#include "Employee.h"
#include "Manager.h"
#include <string>
int main()
{
Employee e1("Robert", 5000);
Employee e2("Sam", 8000);
e1.displayDetails();
e2.displayDetails();
Manager m("Ben", 15000, "Marketing");
m.displayDetails();
return 0;
}
英文:
I am a beginner trying to practice the concept of Inheritance and tried building a piece of code but I seem to get this error. Maybe adding the virtual function is messing up my code? Here are my header and source files below:
-
Employee.h
#pragma once #include<string> class Employee { public: std::string name{}; int salary{}; Employee(std::string name, int salary); virtual void displayDetails() {} };
-
Manager.h:
#pragma once #include<string> #include "Employee.h" class Manager : public Employee { private: std::string department{}; public: Manager(std::string name, int salary, std::string department); void displayDetails() override {} };
-
Employee.cpp
#include<iostream> #include "Employee.h" #include<string> Employee::Employee(std::string name, int salary) : name(name), salary(salary) {} void Employee::displayDetails() { std::cout << "Name: " << name << std::endl; std::cout << "Salary: " << salary << std::endl; }
-
Manager.cpp:
#include<iostream> #include<string> #include "Manager.h" Manager::Manager(std::string name, int salary, std::string department) : Employee(name, salary), department(department) {}; void Manager::displayDetails(){ std::cout << "Name: " << name << std::endl; std::cout << "Salary: " << salary << std::endl; std::cout << "Department: " << department << std::endl; }
-
Main.cpp:
#include "Employee.h" #include "Manager.h" #include<string> int main() { Employee e1("Robert", 5000); Employee e2("Sam", 8000); e1.displayDetails(); e2.displayDetails(); Manager m("Ben", 15000, "Marketing"); m.displayDetails(); return 0; }
答案1
得分: 3
在Employee.h
中的这一行:
virtual void displayDetails() {}
实现了 displayDetails
。函数的“主体”是紧随其签名后面的括号中的内容。
因此,当您尝试在.cpp
中“定义”它时,编译器会认为您试图重新定义主体,因为您已经在头文件中提供了一个。
您尝试做的正确语法是:
virtual void displayDetails();
// ^~~~~~~~~~~~
这是一个函数_声明_(没有主体,即没有_定义_)。
当然,对于Manager.h
/Manager.cpp
,答案也是一样的。
英文:
This line in Employee.h
:
virtual void displayDetails() {}
Implements displayDetails
. The "body" of a function is what's in the brackets that follows its signature.
Therefore, when you attempt to "define" it in the .cpp
, the compiler thinks you're trying to redefine the body because you already gave one in the header.
The correct syntax for what you're trying to do is
virtual void displayDetails();
// ^~~~~~~~~~~~
Which is a function declaration (no body, i.e. no definition).
And of course, the answer is the same for Manager.h
/Manager.cpp
.
答案2
得分: 2
使用以下内容来代替:
通过以下方式,您已经*定义*(实现)了该函数。
然后,您再次*定义*(实现)它,这是不可能的。
您应该只在类中*声明*该函数:
virtual void displayDetails();
<details>
<summary>英文:</summary>
With
virtual void displayDetails() {}
you already have *defined* (implemented) the function.
Then you define (implement) it *again*, which isn't possible.
You should only *declare* the function in the class:
virtual void displayDetails();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论