我在底层代码(kontener.h,函数void List :: push)中有问题,这似乎是一个简单的错误,但是我现在呆了几个小时。 我有另一个标头,但未在此处发布,因为我认为该标头对问题没有影响(可能是错误的)
MAIN.CPP
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <fstream>
#include "student.h"
#include "kontener.h"
using namespace std;
int main()
{
List < Student > S;
fstream GRA("GRA.txt", ios::in);
if (!GRA.good())
throw logic_error("Nie znaleziono pliku GRA.TXT");
fstream SID("SID.txt", ios::in);
if (!SID.good())
throw logic_error("Nie znaleziono pliku SID.TXT");
while (!GRA.eof())
{
string nazwisko;
string imie;
string imie2;
int index;
int ocena;
GRA >> nazwisko;
GRA.ignore();
GRA >> imie;
;
GRA.ignore();
GRA >> imie2;
GRA.ignore();
GRA >> index;
GRA.ignore();
GRA >> ocena;
try
{
Student s1;
s1.setNazwisko(nazwisko);
s1.setImie(imie);
s1.setImie2(imie2);
s1.setIndex(index);
s1.setOcena(ocena);
S.push(s1);
}
catch (exception &e)
{
cout << e.what() << endl;
}
}
GRA.close();
SID.close();
fstream INF("INF.TXT", ios::out);
int counter = S.size();
for (int i = 0; i < counter; i++)
{
Student s1 = S.pop();
INF << s1.getIndex() << ";" << s1.getOcena() << ";" << s1.getNazwisko()
<< " " << s1.getImie() << "." << s1.getImie2();
}
INF.close();
}
Kontener.h(此文件中有错误) 错误:“ operator>”不匹配(操作数类型为“ Student”和“ Student”
#ifndef KONTENER_H_INCLUDED
#define KONTENER_H_INCLUDED
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
template<typename T>
struct List
{
private:
struct Node
{
T value;
Node *next;
Node(T v, Node *n = nullptr)
{
value = v;
next = n;
}
};
Node *head;
int counter;
public:
List();
~List();
T pop();
void push(T x);
int size() const;
};
template<typename T>
List<T>::List()
{
head = nullptr;
counter = 0;
}
template<typename T>
List<T>::~List()
{
while (head != nullptr)
{
Node *killer = head;
head = head->next;
delete killer;
}
counter = 0;
}
template<typename T>
T List<T>::pop()
{
if (counter != 0)
{
Node *killer = head;
T x = killer->value;
head = head->next;
delete killer;
counter--;
return x;
}
else
throw logic_error("Empty list.");
}
template<typename T>
int List<T>::size() const
{
return counter;
}
template<typename T>
void List<T>::push(T x)
{
Node *pred = nullptr;
Node *succ = head;
while (succ!=0 && succ->value > x) // ERROR OCCURS HERE
{
pred=succ;
succ=succ->next;
}
Node *creator = new Node(x, succ);
if (pred == nullptr)
{
head = creator;
}
else
{
pred->next = creator;
}
counter++;
}
#endif // KONTENER_H_INCLUDED
答案 0 :(得分:0)
succ->value
和x
的类型为Student
。您正在尝试对这些操作符使用运算符,尤其是运算符>
。您的编译器告诉您,没有为两个operator>
类型的值定义Student
。
您需要自己实施操作员。
此类操作员的签名如下:
inline bool operator> (const Student& s1, const Student& s2);
或作为Student
类本身的成员:
inline bool operator> (const Student& other) const;
您还可以先搜索您的项目,以检查运算符是否确实在某个地方实现了,以及您只是没有包含适当的标头。