运算符重载
我正在从事这个项目只是想保持我的 C++ 知识。无论如何,当我尝试实现运算符重载时,我遇到了很多很多错误。不知道为什么。
#include "students.h"
#include <iostream>
#include "Quack.h"
using namespace std;
void main()
{
quack* classmates = new quack;
classmates->pushFront(students("corey", "9081923456", 4.0));
cout << "\noriginal data set -- " << *students;
这就是我收到操作员错误的地方。奇怪的是,如果我注释掉重载运算符并将其保留在 Students.cpp 中,它会编译 find。
#ifndef STUDENTS_H
#define STUDENTS_H
#include <iostream>
class students
{
// causing errors
friend ostream& operator << (ostream& out,const students& student);
public:
students();
students(char * name, char* oitId, float gpa);
students(const students& student); // copy constructor;
~students();
const students& operator=(const students& student);
void getName(char* name) const;
void getoitId(char* oitId) const;
float getGpa(void) const;
void setName(char* name);
void setoitId(char* oitId);
void setGpa(float gpa);
private:
char* name;
char* oitId;
float gpa;
};
#endif
}
并且,也会导致错误。但不是单独的..
#include "students.h"
#include <iostream>
#include <iomanip>
using namespace std;
#pragma warning(disable:4996)
private:
char* name;
char* oitId;
float gpa;
students::students(): name(NULL), oitId(NULL), gpa(0)
{
}
students::students(char *name, char *oitId, float gpa): name(NULL), oitId(NULL), gpa(0)
{
setName(name);
setoitId(oitId);
}
students::~students()
{
if(name)
delete[] name;
if(oitId)
delete[] oitId;
}
const students& students::operator=(const students& student)
{
//if it is a self copy, don't do anything
if(this == &student)
return *this;
//make current object *this a copy of the passed in student
else
{
setName(student.name);
setoitId(student.oitId);
//setGpa(student.gpa);
return *this;
}
}
void students::setName(char *name)
{
//release the exisisting memory if there is any
if(this->name)
delete [] this->name;
//set new name
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
void students::setoitId(char *oitId)
{
if(this->oitId)
delete [] this->oitId;
//set new Id
this->oitId = new char[strlen(oitId)+1];
strcpy(this->oitId, oitId);
}
ostream& operator<< (ostream& out, const students& student)
{
//out << setw(20) << student.name
//<< setw(15) << student.pccId
//<< setw(8) << fixed << setprecision(2) << student.gpa;
return out;
}
这是我得到的错误
语法错误:缺少“;”在“&”之前
:错误 C2433:“ostream”:数据声明中不允许使用“friend”
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int
错误 C2061:语法错误:标识符“ostream”
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持default-int
错误 C2805:二进制“运算符 <<”参数太少
1>生成代码...
1>编译...
1>学生.cpp
我的眼睛在燃烧,我不明白为什么它对重载的运算符不满意。
I am working on this project just trying to keep up my c++ knowledge. Anyways, I am getting many, many errors when i try to implement an operator overload. Not sure why.
#include "students.h"
#include <iostream>
#include "Quack.h"
using namespace std;
void main()
{
quack* classmates = new quack;
classmates->pushFront(students("corey", "9081923456", 4.0));
cout << "\noriginal data set -- " << *students;
and this is where i am getting the errors with the operator. Oddly enough if i comment out the overloaded operator and leave it in students.cpp it compiles find.
#ifndef STUDENTS_H
#define STUDENTS_H
#include <iostream>
class students
{
// causing errors
friend ostream& operator << (ostream& out,const students& student);
public:
students();
students(char * name, char* oitId, float gpa);
students(const students& student); // copy constructor;
~students();
const students& operator=(const students& student);
void getName(char* name) const;
void getoitId(char* oitId) const;
float getGpa(void) const;
void setName(char* name);
void setoitId(char* oitId);
void setGpa(float gpa);
private:
char* name;
char* oitId;
float gpa;
};
#endif
}
and, causes errors alo. But not by itself..
#include "students.h"
#include <iostream>
#include <iomanip>
using namespace std;
#pragma warning(disable:4996)
private:
char* name;
char* oitId;
float gpa;
students::students(): name(NULL), oitId(NULL), gpa(0)
{
}
students::students(char *name, char *oitId, float gpa): name(NULL), oitId(NULL), gpa(0)
{
setName(name);
setoitId(oitId);
}
students::~students()
{
if(name)
delete[] name;
if(oitId)
delete[] oitId;
}
const students& students::operator=(const students& student)
{
//if it is a self copy, don't do anything
if(this == &student)
return *this;
//make current object *this a copy of the passed in student
else
{
setName(student.name);
setoitId(student.oitId);
//setGpa(student.gpa);
return *this;
}
}
void students::setName(char *name)
{
//release the exisisting memory if there is any
if(this->name)
delete [] this->name;
//set new name
this->name = new char[strlen(name)+1];
strcpy(this->name, name);
}
void students::setoitId(char *oitId)
{
if(this->oitId)
delete [] this->oitId;
//set new Id
this->oitId = new char[strlen(oitId)+1];
strcpy(this->oitId, oitId);
}
ostream& operator<< (ostream& out, const students& student)
{
//out << setw(20) << student.name
//<< setw(15) << student.pccId
//<< setw(8) << fixed << setprecision(2) << student.gpa;
return out;
}
here is the errors i get
syntax error : missing ';' before '&'
: error C2433: 'ostream' : 'friend' not permitted on data declarations
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2061: syntax error : identifier 'ostream'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2805: binary 'operator <<' has too few parameters
1>Generating Code...
1>Compiling...
1>students.cpp
my eyes are burning and i cant figure out why its unhappy with the overloaded operator..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
ostream
,而没有使用命名空间std::
对其进行限定。编译器错误/警告模糊地告诉您它遇到了尚未声明的类型。
You are using
ostream
without qualifying it with namespacestd::
The compiler errors/warnings are vaguely telling you that it has encountered a type that has not been declared yet.
与 sbi 的评论一致,如果您的目标是提高您的 C++ 知识:
现在,具体查看您的代码:
1.我假设这
是一个拼写错误, students 是一种类型,您只能取消引用指针,但这不是(至少在您发布的代码片段中 - 如果不是,仔细考虑变量名称!)。
2. 大概你的意思是
如果是这样,请检查你的流操作符是否正确实现了。
Inline with comment by sbi, if your goal is to improve your C++ knowledge:
Now, looking specifically at your code:
1. I'll assume that
is a typo, students is a type, you can only dereference a pointer, which this isn't (at least in the code snippet you've posted - if not, think carefully about variable names!).
2. presumably you meant it to be
If so, check your stream operator for quack is implemented correctly.