运算符重载

发布于 2024-09-26 14:12:36 字数 3017 浏览 1 评论 0原文

我正在从事这个项目只是想保持我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-10-03 14:12:36

您正在使用 ostream ,而没有使用命名空间 std:: 对其进行限定。

编译器错误/警告模糊地告诉您它遇到了尚未声明的类型。

friend std::ostream& operator << (std::ostream& out,const students& student);

You are using ostream without qualifying it with namespace std::

The compiler errors/warnings are vaguely telling you that it has encountered a type that has not been declared yet.

friend std::ostream& operator << (std::ostream& out,const students& student);
扭转时空 2024-10-03 14:12:36

与 sbi 的评论一致,如果您的目标是提高您的 C++ 知识:

  1. 考虑使用 C++ 对象 - 例如 std::string 等,而不是 char* (那么您不需要显式内存管理 - 始终是一个不错的选择对于 bug 和奇怪的崩溃),你的 getters 可以返回一个对 std::string 的 const 引用(大概在你的实现代码中,你正在做一个副本,考虑一下这个的附加问题......)
  2. 大概庸医是一些类型的容器,再次强调第 1 点,考虑使用一些现有的库(例如 STL)。
  3. “这个->”是多余的,可以直接引用类成员 - 只是使代码变得混乱,如果担心变量名称冲突,请考虑命名约定并坚持使用。

现在,具体查看您的代码:
1.我假设这

cout << "\noriginal data set -- " << *students;

是一个拼写错误, students 是一种类型,您只能取消引用指针,但这不是(至少在您发布的代码片段中 - 如果不是,仔细考虑变量名称!)。
2. 大概你的意思是

cout << "\noriginal data set -- " << *classmates;

如果是这样,请检查你的流操作符是否正确实现了。

Inline with comment by sbi, if your goal is to improve your C++ knowledge:

  1. consider using C++ objects - such as std::string etc. rather than the char* (then you don't need the explicit memory management - always a good candidate for bugs and weird crashes), your getters can then return a const reference to the std::string (presumably in your implementation code, you're doing a copy, think about the added issues of this...)
  2. Presumably quack is some kind of container, again point 1, think about using some of the existing libraries (STL for example).
  3. "this->" is redundant, class members can be referred to directly - just clutters the code, if worried about variable name clashes, think about a naming convention and stick to it.

Now, looking specifically at your code:
1. I'll assume that

cout << "\noriginal data set -- " << *students;

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

cout << "\noriginal data set -- " << *classmates;

If so, check your stream operator for quack is implemented correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文