gpt4 book ai didi

c++ - 如何将用户定义的类用作模板类的一种类型,并正确地为这两个类重载 <<?

转载 作者:行者123 更新时间:2023-11-28 03:04:56 27 4
gpt4 key购买 nike

首先,我希望我能很好地解释这一点。我想要完成的是创建一个 Student 和 Winner(来自他们的类(class))的 Trio(模板类(class))。另外,我试图重载 Student 和 Winner 类中的 << 运算符,以便可以使用 cout 输出它们。目前,我遇到的问题是编译器为 Student 和 Winner 重载 << 运算符提供了一个链接错误。感谢您的帮助。

感谢进阶。

这是我目前的代码:

#include <algorithm>;
#include <cstdlib>;
#include <ctime>;
#include <deque>;
#include <iostream>;
#include <list>;
#include <stack>;
#include <string>;
#include <set>;


using namespace std;

template <class T>
class Trio
{
public:
Trio();
Trio(T first_value, T second_value, T third_value);
void set_element(int position, T value);
T get_element(int position) const;
void set_all (T first_value, T second_value, T third_value);
private:
T first;
T second;
T third;

friend ostream& operator << (ostream& os, const Trio<T>& x)
{
os << "Trio Values: " << x.first << ", " << x.second << ", " << x.third;
return os;
}
};

class Student
{
public:
Student();
Student(string name_value, double gpa_value);
private:
double gpa;
string name;
friend ostream& operator << (ostream& os, const Student<T>& a)
{
os << "Name: " << a.name << endl;
os << "GPA: " << a.gpa << endl;
return os;
}
};

class Winner
{
public:
Winner();
Winner(string competition_value, string name_value);
private:
string competition;
string name;
friend ostream& operator << (ostream& os, const Winner& a)
{
os << "Competition: " << a.competition << endl;
os << "Name: " << a.name << endl;
return os;
}
};

int main()
{
Trio <int> xyz_coordinates(1,2,3);
Trio <char> name_initials('D','J','C');
Trio <string> full_name("Bill", "Tom", "Smith");
Trio <string> car("GMC", "Sierra 1500", "5058dc");
Student s1("Bill", 4.0), s2("Mary", 3.98), s3("Joe", 3.95);
Winner w1("NBA Champions", "Celtics"), w2("Superbowl Winners", "Patriots"), w3("World Series", "Red Sox");
Trio <Student> top_students(s1,s2,s3);
Trio <Winner> winners(w1,w2,w3);

xyz_coordinates.set_element(1, 0);
cout << xyz_coordinates << endl;
cout << "Value: " << xyz_coordinates.get_element(1) << endl;
xyz_coordinates.set_all(1,2,3);
cout << xyz_coordinates << endl;
cout << endl;

name_initials.set_element(1, 'A');
cout << name_initials << endl;
cout << "Value: " << name_initials.get_element(1) << endl;
name_initials.set_all('D','J','C');
cout << name_initials << endl;
cout << endl;

full_name.set_element(1, "Dere");
cout << full_name << endl;
cout << "Value: " << full_name.get_element(1) << endl;
full_name.set_all("Derek","John","Campaniello");
cout << full_name << endl;
cout << endl;

car.set_element(3, "5897av");
cout << car << endl;
cout << "Value: " << car.get_element(2) << endl;
car.set_all("Ford","F150","3465d");
cout << car << endl;

cout << top_students << endl;
cout << winners << endl;

/*************************************/
/* List of Ints */
/*************************************/
cout << endl;
list <int> alist;
list<int>::const_iterator listiter1;

alist.push_back(17);
alist.push_back(87);
alist.push_back(22);
alist.push_back(26);
alist.push_back(24);

cout << "List A: \n";
for (listiter1 = alist.begin(); listiter1 != alist.end(); listiter1++)
{
cout << *listiter1 << " ";
}
cout << endl;

alist.pop_back();

cout << "List A after popping back: \n";
for (listiter1 = alist.begin(); listiter1 != alist.end(); listiter1++)
{
cout << *listiter1 << " ";
}
cout << endl;

alist.sort();
cout << "List A after being sorted: \n";
for (listiter1 = alist.begin(); listiter1 != alist.end(); listiter1++)
{
cout << *listiter1 << " ";
}
cout << endl;

cout << endl;

/*************************************/
/* Deque of Chars */
/*************************************/
deque <char> adeque;
deque<char>::const_iterator dequeiter1;

adeque.push_back('A');
adeque.push_back('B');
adeque.push_back('C');
adeque.push_back('D');

cout << "Deque A: \n";
for (dequeiter1 = adeque.begin(); dequeiter1 != adeque.end(); dequeiter1++)
{
cout << *dequeiter1 << " ";
}
cout << endl;

adeque.pop_front();
cout << "Deque A after being popped in the front: \n";
for (dequeiter1 = adeque.begin(); dequeiter1 != adeque.end(); dequeiter1++)
{
cout << *dequeiter1 << " ";
}
cout << endl;

cout << "Deque A after being randomized: \n";
srand(time(0));
random_shuffle(adeque.begin(), adeque.end());
for (dequeiter1 = adeque.begin(); dequeiter1 != adeque.end(); dequeiter1++)
{
cout << *dequeiter1 << " ";
}
cout << endl;


/*************************************/
/* Set of Strings */
/*************************************/

cout << endl;
set <string> aset;
set <string> bset;
set<string>::const_iterator iter1;

aset.insert("ABC");
aset.insert("DEF");
aset.insert("GHI");

bset.insert("RST");
bset.insert("UVW");
bset.insert("XYZ");

cout << "Set A contains: \n";

for (iter1 = aset.begin(); iter1 != aset.end(); iter1++)
{
cout << *iter1 << " ";
}
cout << endl;

cout << "Set A with DEF Erased: \n";
aset.erase("DEF");
for (iter1 = aset.begin(); iter1 != aset.end(); iter1++)
{
cout << *iter1 << " ";
}
cout << endl;

aset.swap(bset);

cout << "Swapped Sets: \n";

cout << "Set A: \n";
for (iter1 = aset.begin(); iter1 != aset.end(); iter1++)
{
cout << *iter1 << " ";
}
cout << endl;

cout << "Set B: \n";
for (iter1 = bset.begin(); iter1 != bset.end(); iter1++)
{
cout << *iter1 << " ";
}
cout << endl;

/*****************************/
/* Stack of Student */
/*****************************/
stack <Student> astack;

astack.push("Student 1");


/*****************************/
/* Queue of Winners */
/*****************************/
cout << endl;
system("pause");
}

template <class T>
Trio<T>::Trio(T first_value, T second_value, T third_value)
{
first = first_value;
second = second_value;
third = third_value;
}

template <class T>
void Trio<T>::set_element(int position, T value)
{
if (position == 1)
{
first = value;
cout << "First value set to: " << value << endl;
}
else if (position == 2)
{
second = value;
cout << "Second value set to: " << value << endl;
}
else if (position == 3)
{
third = value;
cout << "Third value set to: " << value << endl;
}
else
{
cout << "Element not set." << endl;
}

}

template <class T>
T Trio<T>::get_element(int position) const
{
if (position == 1)
{
return first;
}
else if (position == 2)
{
return second;
}
else if (position == 3)
{
return third;
}
else
{
T null;
return null;
}
}

template <class T>
void Trio<T>::set_all (T first_value, T second_value, T third_value)
{
first = first_value;
second = second_value;
third = third_value;
cout << "Values set as follows: " << first << ", " << second << ", " << third << endl;
}

Student::Student(string name_value, double gpa_value)
{
name = name_value;
gpa = gpa_value;
}

Winner::Winner(string competition_value, string name_value)
{
competition = competition_value;
name = name_value;
}

最佳答案

这里是明显的错误:

  1. #include指令接受终止分号,尽管编译器可能只对这个问题发出警告。
  2. Student不是模板,即有多余的 <T>Student 的声明中输出运算符。
  3. 声明astack.push("Student 1");不会编译因为 Student实际上需要两个构造函数参数而不是一个。您需要显式构造 Student对象。
  4. Student 有默认构造函数和 Winner已声明但未定义。

修复问题后,代码应该可以编译和链接。

关于c++ - 如何将用户定义的类用作模板类的一种类型,并正确地为这两个类重载 <<?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967945/

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