C++ 使用函数从文件输出

C++ 使用函数从文件输出,c++,visual-studio,C++,Visual Studio,标题说明了一切 这是我的代码: 我有一个叫做House的类,我在其中定义值 class HOUSE { public: int id; string 1; string 2; string 3; int an; }; template<class Type> class table { public: vector<Type> V; //double inceput; //double sfirsit

标题说明了一切

这是我的代码: 我有一个叫做House的类,我在其中定义值

class HOUSE
{
public:
    int id;
    string 1;
    string 2;
    string 3;
    int an;


};

template<class Type>
class table
{
public:

    vector<Type> V;
    //double inceput;
    //double sfirsit;
    //int comparatii;
    //int interschimbari;   
public:
    table();
    void print();
    void liniar();
};

template<class Type>
table<Type>::table()
{
    ifstream file("file.txt");
    ifstream file1("file1.txt");

    if (file.fail() || file1.fail())
    {
        cerr << "Eroare la deschiderea fisierului!" << endl;
        _getch();
        exit(1);
    }

    HOUSE* value = new HOUSE;

while (!file.eof() || file1.fail())
{
    file >> value->id;
    file >> value->tara;
    file >> value->brand;
    file >> value->culoare;
    file >> value->an;

    this->V.push_back(*value);
}


file.close();
阶级之家
{
公众:
int-id;
字符串1;
字符串2;
字符串3;
INTAN;
};
模板
类表
{
公众:
向量V;
//双输入;
//双sfirsit;
//国际比较二;
//int interschimbari;
公众:
表();
作废打印();
虚线();
};
模板
表::表()
{
ifstream文件(“file.txt”);
ifstream file1(“file1.txt”);
if(file.fail()| | file1.fail())
{
cerr值->id;
文件>>值->塔拉;
文件>>价值->品牌;
文件>>值->culoare;
文件>>value->an;
此->V.push_back(*值);
}
file.close();
}

值的打印函数

template<class Type>
void table<Type>::print()
{

    cout << endl << setw(50) << "AFISAREA DATELOR" << endl;
    cout << setw(5) << "Id" << setw(15) << "1" << setw(20) << "2" << setw(17) << "3" << setw(20) << "an" << endl << endl;
    for (int i = 0; i < this->V.size(); i++)
    {
        cout << setw(5) << this->V.at(i).id << setw(15)
            << this->V.at(i).1<< setw(17)
            << this->V.at(i).2<< setw(17)
            << this->V.at(i).3<< setw(25)
            << this->V.at(i).an << endl;
    }
    cout << endl << "Dimensiunea tabelului  n= " << V.size() << endl;

}


{
        file >> value->id;
        file >> value->1;
        file >> value->2;
        file >> value->3;
        file >> value->an;

        this->V.push_back(*value);
    }


    file.close();

}
模板
void表::print()
{

cout你的代码有很多错误。但我将忽略这一点,只回答我认为是你真正的问题

您需要两个
table
对象,其中一个从
file.txt
读取,另一个从
file1.txt
读取。为此,您应该将文件名传递到
table::table
构造函数中,以便它知道从哪个文件读取

template<class Type>
class table
{
    ...
public:
    table(const char* filename); // constructor takes filename parameter
    ...
};


template<class Type>
table<Type>::table(const char* filename)
{
    ifstream file(filename); // open filename
    if (file.fail())
...


int main() {

    table<MOBILE>* file = new table<MOBILE>("file.txt"); // read from file.txt
    table<MOBILE>* file1 = new table<MOBILE>("file1.txt"); // read from file1.txt

    file ->print();    
    file1 ->print();
}
模板
类表
{
...
公众:
表(const char*filename);//构造函数接受filename参数
...
};
模板
表::表(常量字符*文件名)
{
ifstream文件(文件名);//打开文件名
if(file.fail())
...
int main(){
table*file=newtable(“file.txt”);//从file.txt读取
table*file1=新表(“file1.txt”);//从file1.txt读取
文件->打印();
file1->print();
}

但仅从第一个文件打印:行为是什么?是否有崩溃停止执行?恐怕标题和问题正文都不能说明一切,甚至不够……您对这段代码有什么疑问?您还没有包括
MOBILE
的代码。我发现无法相信这段代码编译时出错,这是一个错误de
string 1;
是一个编译错误。似乎还存在其他错误。如果要编写代码以打印文件
filetxt
file1.txt
,则需要将要使用的文件名传递到
table
构造函数中。table*file=new table();table*file1=new table();应该是table*file=new table();table*file1=new table();我无法同时从两个文件中输出内容。file->print();file1->print();都从文件输出内容,而文件和文件1有不同的内容谢谢!这解决了我的问题。关于其他战争,是关于模板的。我不担心它们。干杯
template<class Type>
class table
{
    ...
public:
    table(const char* filename); // constructor takes filename parameter
    ...
};


template<class Type>
table<Type>::table(const char* filename)
{
    ifstream file(filename); // open filename
    if (file.fail())
...


int main() {

    table<MOBILE>* file = new table<MOBILE>("file.txt"); // read from file.txt
    table<MOBILE>* file1 = new table<MOBILE>("file1.txt"); // read from file1.txt

    file ->print();    
    file1 ->print();
}