尽管使用了包含警卫和编译指示

时间:2018-03-01 02:14:54

标签: c++ class header redefinition

我在我的c ++项目中的两个.cpp文件中遇到两个类类型重定义错误。我正在使用Visual Studio 2017.我已经搜索了互联网寻找解决方案,并发现一群人说要使用包括警卫和#pragma一次。我在我的头文件中加入了包含防护,但我仍然在.cpp文件中遇到类重定义错误。我还尝试将#pragma一次放在项目中所有文件的顶部。这是父类中的代码(这是用于处理继承的赋值)

#pragma once

#include "stdafx.h"
#include "Members.h"
#include <string>
#include <iostream>
#include <fstream>

class Members
{
protected:
    string first;
    string last;
    int ID;
public:
    Members() {};

    Members(const string& firstName, const string& lastName, int idNum)
    {
        first = firstName;
        last = lastName;
        ID = idNum;
    }

    void printMember() const
    {
        cout << "First Name is:" << first << " Last Name is:" << last << " 
ID is:" << ID << endl;
}

void readWriteMembers(ifstream& instream, ofstream& outstream)
{
    string x;
    for (int i = 0; instream.good(); i++)
    {
        getline(instream, x);
        outstream << x;
    }
}

string getFirstName()
{
    return first;
}

void setFirstName(string firstName)
{
    first = firstName;
}

string getLastName()
{
    return last;
}

void setLastName(string lastName)
{
    last = lastName;
}

int getID()
{
    return ID;
}

void setID(int id)
{
    ID = id;
}
};

以下是子类的代码。

#pragma once
#include "stdafx.h"
#include "Faculty.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Faculty:public Members
{
private:
    double salary;
public:
    Faculty() {};

    Faculty(const string& firstName, const string& lastName, int idNum, double theSalary) :Members(firstName,lastName,idNum)
{
    salary = theSalary;
}

void printMember()
{
    cout << "The faculty member name is:" << first << " " << last << " The id is:" << ID << " and the salary is:" << salary << endl;
}

double getSalary()
{
    return salary;
}

void setSalary(double theSalary)
{
    salary = theSalary;
}
void readWriteMembers(ifstream& instream)
{
    ofstream faculty_output;
    string firstname;
    string lastname;
    string id;
    string salary;
    faculty_output.open("FacultyOutput.txt");
    for (int i = 0; instream.good(); i++)
    {
        if (instream.get() == 'f')
        {
            instream.ignore(1);
            instream >> firstname;
            instream >> lastname;
            instream >> id;
            instream >> salary;
            cout << "Faculty name:" << firstname << " " << lastname << "id is:" << id << " Salary is:" << salary << endl;

        }
    }
    faculty_output.close();
}
};

如果有帮助,这里是两个相应头文件的代码。

#pragma once
#ifndef MEMBERS_H
#define MEMBERS_H
#include <string>
using namespace std;
class Members
{
protected:
    string first;
    string last;
    int ID;
public:

    Members() {};
Members(const string& firstName, const string& lastName, int idNum) {};
void printMember() const {};
void readWriteMembers(ifstream& instream, ofstream& outstream);
string getFirstName();
void setFirstName(string firstName);
string getLastName();
void setLastName(string lastName);
int getID();
void setID(int id);
};
#endif

子类的头文件。

#pragma once
#ifndef FACULTY_H
#define FACULTY_H
#include "Members.h"
using namespace std;
class Faculty:public Members
{
private:
double salary;
public:
Faculty() {};
Faculty(const string& firstName, string& lastName, int id, double salary) 
{};
void printMember() {};
void readWriteMembers(ifstream& instream) {};
double getSalary() {};
void setSalary(double theSalary) {};
};
#endif

0 个答案:

没有答案