C++中对象的处理方式
Way to handle the objects in C++
每次我 运行 代码编译器都会给出对象已经定义的错误,我不知道我在整个代码中哪里出错了。
即使我在一个文件中完成所有这些工作,它也能正常工作,但我不知道为什么它不能以这种方式工作,任何人都可以帮助我解决我在这段代码中做错的地方。
如有任何帮助,我们将不胜感激。
谢谢
student.h
ifndef STUDENT
define STUDENT
class Student
{
public:
char student_no[10];
char student_name[20];
char student_address[20];
char student_score[20];
Student();
};
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
#endif
student.cpp
using namespace std;
#include "writestr.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
int main(){
char filename[20];
Student s;
cout << "Enter the file name:" << flush;
cin.getline(filename, 19);
ofstream stream(filename, ios::out);
if (stream.fail()) {
cout << "File open failed!" << endl;
return 0;
}
while (1) {
cin >> s; // read fields of person
if (strlen(s.student_name) == 0) break;
// write person to output stream
stream << s; // write fields of person
}
return 1;
}
Problems occured
这是我编写流代码的部分。
writestr.cpp
using namespace std;
#include "readper.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
ostream & operator << (ostream & stream, Student & s)
{ // insert fields into file
stream << s.student_name << s.student_no << s.student_address
<< s.student_score;
return stream;
}
readper.cpp
using namespace std;
#include "student.h"
#include <fstream>
#include <string.h>
#include <iostream>
istream & operator >> (istream & stream, Student & s)
{ // read fields from input
cout << "Enter Student Name, or <cr> to end: " << flush;
stream.getline(s.student_name, 30);
if (strlen(s.student_name) == 0) return stream;
cout << "Enter Student Name: " << flush; stream.getline(s.student_name, 30);
cout << "Enter Student Id Number: " << flush; stream.getline(s.student_no, 30);
cout << "Enter Address: " << flush; stream.getline(s.student_address, 30);
cout << "Enter Score: " << flush; stream.getline(s.student_score, 15);
return stream;
}
您正在定义(不仅仅是声明)头文件中的构造函数:
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
这在包含头文件的每个 cpp 中一次又一次地定义构造函数(生成代码)。由于这个定义没有 inline
关键字,它可能在程序中只存在一次,而不是多次。在多个翻译单元(cpp 文件)中定义非内联构造函数导致错误。
可能的解决方案:
- 将构造函数定义移动到 class 或
- 使用
inline
关键字作为前缀,或者
- 将其移动到其中一个 cpp 文件
另一个问题:您包含 cpp 文件,这会通过一次又一次地声明相同的内容而导致更多问题。只需将它们添加到 project/makefile/etc,而不是包括:
#include "writestr.cpp"
每次我 运行 代码编译器都会给出对象已经定义的错误,我不知道我在整个代码中哪里出错了。
即使我在一个文件中完成所有这些工作,它也能正常工作,但我不知道为什么它不能以这种方式工作,任何人都可以帮助我解决我在这段代码中做错的地方。
如有任何帮助,我们将不胜感激。 谢谢
student.h
ifndef STUDENT
define STUDENT
class Student
{
public:
char student_no[10];
char student_name[20];
char student_address[20];
char student_score[20];
Student();
};
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
#endif
student.cpp
using namespace std;
#include "writestr.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
int main(){
char filename[20];
Student s;
cout << "Enter the file name:" << flush;
cin.getline(filename, 19);
ofstream stream(filename, ios::out);
if (stream.fail()) {
cout << "File open failed!" << endl;
return 0;
}
while (1) {
cin >> s; // read fields of person
if (strlen(s.student_name) == 0) break;
// write person to output stream
stream << s; // write fields of person
}
return 1;
}
Problems occured
这是我编写流代码的部分。
writestr.cpp
using namespace std;
#include "readper.cpp"
#include <fstream>
#include <string.h>
#include <iostream>
ostream & operator << (ostream & stream, Student & s)
{ // insert fields into file
stream << s.student_name << s.student_no << s.student_address
<< s.student_score;
return stream;
}
readper.cpp
using namespace std;
#include "student.h"
#include <fstream>
#include <string.h>
#include <iostream>
istream & operator >> (istream & stream, Student & s)
{ // read fields from input
cout << "Enter Student Name, or <cr> to end: " << flush;
stream.getline(s.student_name, 30);
if (strlen(s.student_name) == 0) return stream;
cout << "Enter Student Name: " << flush; stream.getline(s.student_name, 30);
cout << "Enter Student Id Number: " << flush; stream.getline(s.student_no, 30);
cout << "Enter Address: " << flush; stream.getline(s.student_address, 30);
cout << "Enter Score: " << flush; stream.getline(s.student_score, 15);
return stream;
}
您正在定义(不仅仅是声明)头文件中的构造函数:
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
这在包含头文件的每个 cpp 中一次又一次地定义构造函数(生成代码)。由于这个定义没有 inline
关键字,它可能在程序中只存在一次,而不是多次。在多个翻译单元(cpp 文件)中定义非内联构造函数导致错误。
可能的解决方案:
- 将构造函数定义移动到 class 或
- 使用
inline
关键字作为前缀,或者 - 将其移动到其中一个 cpp 文件
另一个问题:您包含 cpp 文件,这会通过一次又一次地声明相同的内容而导致更多问题。只需将它们添加到 project/makefile/etc,而不是包括:
#include "writestr.cpp"