- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我希望我能很好地解释这一点。我想要完成的是创建一个 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;
}
最佳答案
这里是明显的错误:
#include
指令不接受终止分号,尽管编译器可能只对这个问题发出警告。Student
不是模板,即有多余的 <T>
在 Student
的声明中输出运算符。astack.push("Student 1");
不会编译因为 Student
实际上需要两个构造函数参数而不是一个。您需要显式构造 Student
对象。Student
有默认构造函数和 Winner
已声明但未定义。修复问题后,代码应该可以编译和链接。
关于c++ - 如何将用户定义的类用作模板类的一种类型,并正确地为这两个类重载 <<?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967945/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!