我不太擅长利用类。我通常可以使用小写的类,但是使用大写的类时,我不明白为什么它无法编译。
我的代码是三个文件:
main.cpp:
#include <iostream>
#include "animal.h"
using namespace std;
int main()
{
return 0;
}
animal.h:
#include <iostream>
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
Animal(std::string aSpecies);
void set_name(std::string aName);
std::string get_species();
std::string get_name();
int get_ID();
int id_num;
std::string name;
};
#endif
animal.cpp:
#include <iostream>
#include "animal.h"
//using namespace std;
Animal::Animal()
{
id_num = 0;
name = 0;
}
Animal(std::string aSpecies)
{
}
void Animal::set_name(std::string aName)
{
}
std::string Animal::get_species()
{
}
std::string Animal::get_name()
{
}
int get_ID()
{
}
现在,它不会打印任何内容,我只希望它编译时没有错误。
错误是:
In file included from main.cpp:2:0:
animal.h:9:23: error: expected ‘)’ before ‘aSpecies’
Animal(string aSpecies);
^~~~~~~~
In file included from animal.cpp:2:0:
animal.h:9:23: error: expected ‘)’ before ‘aSpecies’
Animal(string aSpecies);
^~~~~~~~
animal.cpp:5:16: error: definition of implicitly-declared ‘Animal::Animal()’
Animal::Animal()
^
animal.cpp:11:20: error: expected ‘)’ before ‘aSpecies’
Animal(std::string aSpecies)
^~~~~~~~
答案 0 :(得分:4)
构造函数Animal::Animal()
未在类中定义。将Animal();
添加到类定义中。
在实现中将Animal(std::string aSpecies)
替换为Animal::Animal(std::string aSpecies)
(animal.cpp)。
将#include <string>
添加到animal.h
以使用字符串。