'Way to handle the objects in C++
Every time when I run the code Compiler give the error of object already define and I don't know where I am making mistakes in the whole code.
Even if I do this all in one file it is working but I don't know why its not working in this style can anyone help me that where I am doing mistake in this code.
Any help would be appreciated. Thank you
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;
}
This is the portion where I write the streaming code.
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;
}
Solution 1:[1]
You are defining (not just declaring) the constructor in the header file:
Student::Student()
{//constructor
student_no[0] = 0; student_name[0] = 0; student_address[0] = 0;
student_score[0] = 0;
}
This defines the constructor (generates the code) again and again in every cpp that includes the header file. Since this definition has no inline
keyword, it may exist exactly once in the program, not multiple times. Defining the non-inline constructor in multiple translation units (cpp files) causes the error.
Possible solutions:
- Move the constructor definition into the class, or
- Prefix it by an
inline
keyword, or - Move it to one of the cpp files
Another issue: you include cpp files, which cause a lot more issues by declaring the same thing again and again. Simply add them to the project/makefile/etc, instead of including:
#include "writestr.cpp"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |