无法解决 C2084 错误:’函数已经有主体’ – C++

huangapple go评论121阅读模式
英文:

Unable to resolve C2084 error: 'Function already has a body' - C++

问题

以下是翻译好的部分:

  1. Employee.h
#pragma once
#include <string>

class Employee {

public:
std::string name{};
int salary{};

Employee(std::string name, int salary);

virtual void displayDetails() {}

};
  1. 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 {}

};
  1. 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;
}
  1. 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;
}
  1. 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:

  1. Employee.h

     #pragma once
     #include&lt;string&gt;
    
     class Employee {
    
     public:
     std::string name{};
     int salary{};
    
     Employee(std::string name, int salary);
    
     virtual void displayDetails() {}
    
     };
    
  2. Manager.h:

     #pragma once
    
     #include&lt;string&gt;
     #include &quot;Employee.h&quot;
    
     class Manager : public Employee {
    
     private:
     std::string department{};
    
     public:
     Manager(std::string name, int salary, std::string department);
     void displayDetails() override {}
    
     };
    
  3. Employee.cpp

    #include&lt;iostream&gt;
    #include &quot;Employee.h&quot;
    #include&lt;string&gt;
    
    Employee::Employee(std::string name, int salary) : name(name), salary(salary) {}
    
    void Employee::displayDetails() {
         std::cout &lt;&lt; &quot;Name: &quot; &lt;&lt; name &lt;&lt; std::endl;
         std::cout &lt;&lt; &quot;Salary: &quot; &lt;&lt; salary &lt;&lt; std::endl;
     }
    
  4. Manager.cpp:

    #include&lt;iostream&gt;
    #include&lt;string&gt;
    #include &quot;Manager.h&quot;
    
    
    Manager::Manager(std::string name, int salary, std::string department) : 
    Employee(name, salary), department(department) {};
    void Manager::displayDetails(){
         std::cout &lt;&lt; &quot;Name: &quot; &lt;&lt; name &lt;&lt; std::endl;
         std::cout &lt;&lt; &quot;Salary: &quot; &lt;&lt; salary &lt;&lt; std::endl;
         std::cout &lt;&lt; &quot;Department: &quot; &lt;&lt; department &lt;&lt; std::endl;
     }
    
  5. Main.cpp:

    #include &quot;Employee.h&quot;
    #include &quot;Manager.h&quot;
    #include&lt;string&gt;
    
    
    int main()
    {
     Employee e1(&quot;Robert&quot;, 5000);
     Employee e2(&quot;Sam&quot;, 8000);
    
     e1.displayDetails();
     e2.displayDetails();
    
     Manager m(&quot;Ben&quot;, 15000, &quot;Marketing&quot;);
     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&#39;t possible.

You should only *declare* the function in the class:

virtual void displayDetails();




</details>



huangapple
  • 本文由 发表于 2023年5月29日 01:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定