gpt4 book ai didi

c++ - 运算符重载

转载 作者:行者123 更新时间:2023-11-30 00:59:42 24 4
gpt4 key购买 nike

我正在从事这个项目,只是想保持我的 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;

这就是我遇到运算符(operator)错误的地方。奇怪的是,如果我注释掉重载运算符并将其留在 students.cpp 中,它会编译查找。

#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;
}

这是我得到的错误

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

我的眼睛在燃烧,我不明白为什么它对重载的运算符不满意..

最佳答案

您正在使用 ostream,但未使用命名空间 std:: 对其进行限定

编译器错误/警告含糊地告诉你它遇到了一个尚未声明的类型。

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

关于c++ - 运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3888139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com