[英]Problem with separating a class into header (.h) and source (.cpp) file
所以我尝试将 class 分为声明和防御,我认为我做得很好,但是当我尝试编译它时,我收到了这个错误消息。 我看不出问题出在哪里,但我怀疑它与简单的语法规则有关。
错误信息
... ...: g++ -o main.exe Dog.cpp main.cpp
Dog.cpp:11:6: error: no declaration matches 'void Dog::setName(int)'
void Dog::setName(int name){
^~~
In file included from Dog.cpp:1:
Dog.h:10:8: note: candidate is: 'void Dog::setName(std::__cxx11::string)'
void setName(string name);
^~~~~~~
Dog.h:6:7: note: 'class Dog' defined here
class Dog{
^~~
Dog.cpp:23:5: error: no declaration matches 'int Dog::getAge()'
int Dog::getAge(){
^~~
In file included from Dog.cpp:1:
Dog.h:11:10: note: candidate is: 'std::__cxx11::string Dog::getAge()'
string getAge();
^~~~~~
Dog.h:6:7: note: 'class Dog' defined here
class Dog{
^~~
这些是使用的文件: main.cpp
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
//Functions
int main(){
//Variables
string userInput;
//Code
Dog dolly("Dolly", 3);
cout<<dolly.getName();
cout<<dolly.getAge();
return 0;
}
狗.h
#ifndef DOG_H
#define DOG_H
#include <string>
using namespace std;
class Dog{
public:
Dog(string name, int age);
string getName();
void setName(string name);
string getAge();
void setAge(int age);
private:
int Age;
string Name;
protected:
};
#endif // DOG_H
狗.cpp
#include "Dog.h"
#include <iostream>
#include <string>
using namespace std;
Dog::Dog(string name, int age){
setName(name);
setAge(age);
};
void Dog::setName(int name){
Name = name;
};
string Dog::getName(){
return Name;
};
void Dog::setAge(int age){
Age = age;
};
int Dog::getAge(){
return Age;
};
提前感谢您的回答!
错误信息非常清楚。
您的 function 签名不匹配。
在你的 header 你声明
void setName(string name);
但是在您的实现文件中,您有
void Dog::setName(int name)
与getAge
相同的问题。 签名不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.