Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;理解类和构造函数 我是C++的新手(来自java)。我在C++中加入类有麻烦。_C++_String_Class_Object_Text - Fatal编程技术网

C++;理解类和构造函数 我是C++的新手(来自java)。我在C++中加入类有麻烦。

C++;理解类和构造函数 我是C++的新手(来自java)。我在C++中加入类有麻烦。,c++,string,class,object,text,C++,String,Class,Object,Text,我在这个程序中的目标是用几个字符串和计数器简单地实现一个基本的动物类 我希望能够从我创建的文本文件中读取,并将文本文件中的行设置为这些变量中的每一个 种类 家庭 门 后代 然后我想让程序打印出所有3门课的结果 我不明白如何实现默认构造函数 这是我的班级 #include <iostream> #include <string> using namespace std; class Animal { string species;

我在这个程序中的目标是用几个字符串和计数器简单地实现一个基本的动物类

我希望能够从我创建的文本文件中读取,并将文本文件中的行设置为这些变量中的每一个

种类 家庭 门 后代

然后我想让程序打印出所有3门课的结果

我不明白如何实现默认构造函数

这是我的班级

#include <iostream>
#include <string>

using namespace std;

class Animal
{
    string species;
                string family;
                string phylum;
                string desc;
                static int count;
   public:    
       bool readIn(ifstream&file, const string frame);
    void printInfo() const;
    void setAnimal(string s, string f, string p, string d);
    static int getCount();
    Animal(string s, string f, string p, string d);
    Animal(ifstream& file, const string fname);
};    
#包括
#包括
使用名称空间std;
类动物
{
弦种;
弦族;
弦门;
字符串描述;
静态整数计数;
公众:
bool readIn(ifstream&file,常量字符串帧);
void printInfo()常量;
void setAnimal(字符串s、字符串f、字符串p、字符串d);
静态int getCount();
动物(串s、串f、串p、串d);
动物(ifstream&file,常量字符串fname);
};    
以下是函数定义:

#include "animal.h"
#include <iostream>
#include <string>
using namespace std;

Animal::Animal(string s, string f, string p, string d)
{
        setAnimal(s,f,p,d);
}    

static int Animal::getCount()

{
    int i=0;
        i++;
        return i;
}

bool Animal::readIn(ifstream &myFile, const string fname)
{
        myFile.open(fname);
        if(myFile)
        {
                getline(myFile, species);
                getline(myFile, family);
                getline(myFile, phylum);
                getline(myFile, desc);
                myFile.close();
                return true;
        } 
        else
        return false;
}   


Animal::Animal(ifstream& file, const string fname)
{
        if(!readIn(file, fname) )
        species="ape";
        family="ape";
        phylum="ape";
        desc="ape";
        count = 1;
}   

void Animal::printInfo() const
{
cout << species << endl;
cout << family << endl;
cout << phylum << endl;
cout << desc << endl;
}

void Animal::setAnimal(string s, string f, string p, string d) 
{
        species = s, family = f, phylum = p, desc = d;
}

int main()
{
        ifstream myFile;
        Animal a;
        Animal b("homo sapien", "primate", "chordata", "erectus");
        Animal c(myFile, "horse.txt");
        a.printInfo();
        b.printInfo();
        c.printInfo();
}
#包括“animal.h”
#包括
#包括
使用名称空间std;
动物::动物(字符串s、字符串f、字符串p、字符串d)
{
刚毛动物(s、f、p、d);
}    
静态int Animal::getCount()
{
int i=0;
i++;
返回i;
}
bool Animal::readIn(ifstream&myFile,常量字符串fname)
{
myFile.open(fname);
如果(我的文件)
{
getline(myFile,物种);
getline(myFile,family);
getline(myFile,门);
getline(myFile,desc);
myFile.close();
返回true;
} 
其他的
返回false;
}   
Animal::Animal(ifstream和file,常量字符串fname)
{
如果(!readIn(文件,fname))
物种=“猿”;
family=“猿”;
门=“猿”;
desc=“ape”;
计数=1;
}   
void Animal::printInfo()常量
{

cout要实现默认构造函数,只需执行您已经完成的操作,但不提供任何参数:

static int getCount();
Animal(string s, string f, string p, string d);
Animal(ifstream& file, const string fname);
Animal(); //Default Constructor
class Animal
{
public:
  Animal() {}; // This is a default constructor
};
Animal a;  // OK
然后在您的实现中:

Animal::Animal(){
    species="ape";
    family="ape";
    phylum="ape";
    desc="ape";
    count = 1;
}

默认构造函数只是一个不带参数的构造函数。如果您没有定义自己的构造函数,编译器会为您生成一个

这个自动生成的函数除了调用类的基和成员的no-param构造函数外,什么都不做


您可以自己定义一个无参数的构造函数。

一个默认构造函数是一个可以在没有指定参数的情况下调用的。这个描述可能有点冗长,所以考虑几个可能性。

通常,或者默认情况下(无双关语),默认构造函数将只是一个不带参数的构造函数:

static int getCount();
Animal(string s, string f, string p, string d);
Animal(ifstream& file, const string fname);
Animal(); //Default Constructor
class Animal
{
public:
  Animal() {}; // This is a default constructor
};
Animal a;  // OK
其他情况下,虽然您可能会编写一个构造器,该构造器不接受参数,但所有参数都有默认值:

class Animal
{
public:
  Animal(int age=42) : age_(age) {};  // This is a default constructor
private:
  int age_;
};
这也是一个默认构造函数,因为可以不使用参数调用它:

static int getCount();
Animal(string s, string f, string p, string d);
Animal(ifstream& file, const string fname);
Animal(); //Default Constructor
class Animal
{
public:
  Animal() {}; // This is a default constructor
};
Animal a;  // OK
您不希望在一个类中有2个默认构造函数。也就是说,不要尝试编写这样的类:

class Animal
{
   public:

      Animal() {};
      Animal(int age=42) : age_(age) {}; 
    private:
      int age_;
};
<>在C++中,如果你有一个没有默认构造函数的类,编译器会自动为你生成一个。但是,如果你已经声明了任何其他构造函数,编译器不会自动生成默认构造函数。因此,在你的情况下,因为你已经声明了2个其他构造函数(都是“转换”)。构造函数),编译器不会为您生成默认构造函数。由于您的类(定义为)没有默认构造函数,因此您不能默认构造
Animal
对象。换句话说,这不会编译:

Animal a;

问题到底是什么?您没有默认构造函数,一旦您提供了自己的构造函数,编译器就会将其删除,除非您显式地将其放到inI中。恐怕您的代码在许多地方都被破坏了。首先,请查看
getCount
-它将始终返回1。您应该首先将
count
变量初始化为0,然后在这个方法只返回总是只
count
@SethCarnegie:是的,你是对的,它会编译,但不会达到预期的效果。我认为最好是删除它,以免打开一罐蠕虫。