操作员<<过载
Operator << overloading
我正在处理我的项目,我想重载运算符<<
,它应该在控制台上打印出我的对象。
对象称为"config",在其模板类中,它有 4 个数组,分别称为attribute1
、attribute2
。attribute3
和attribute4
.
到目前为止,我已经尝试过这个:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
template<typename T> class config
{
T *attribute1, *attribute2, *attribute3, *attribute4;
string attribName1, attribName2, attribName3, attribName4;
public:
config(void)
{
attribute1 = new T[3];
attribute2 = new T[3];
attribute3 = new T[3];
attribute4 = new T[3];
}
~config(void)//destruktor
{
delete [] attribute1, attribute2, attribute3, attribute4;
}
//operatory
friend ostream& operator<<(ostream &out, const config<T> &c);
};//class ends
template <typename T> ostream& operator<<(ostream &out, const config<T> &c)
{
for(int i=0;i<3;i++)
{
out<<c.attribute1[i]<<c.attribute2[i]<<c.attribute3[i]<<c.attribute2[i];
}
return out;
}
每当我尝试编译它时,它都会给出某种奇怪的错误,但我知道问题出在运算符上。
这是它给出的错误:
Error 1 error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class config<double> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$config@N@@@Z) D:DocsVisual Studio 2010Projectsproject_v2.obj project_v2
和:
Error 2 error LNK1120: 1 unresolved externals D:DocsVisual Studio 2010Projectsproject_v2.exe project_v2
并且未指定行或列。
这是解决问题的一种方法。
-
在定义
config
之前,先声明operator<<
函数。为了声明函数,您必须转发声明类模板。template <typename T> class config; template <typename T> std::ostream& operator<<(std::ostream &out, const config<T> &c);
-
在类中,通过使用
T
作为模板参数使函数成为友元函数。friend ostream& operator<< <T>(ostream &out, const config &c); // Notice this ^^^^ // This makes sure that operator<< <int> is not a friend of // config<double>. Only operator<< <double> is a friend of // config<double>
以下是包含这些更改的工作程序:
#include <string>
#include <iostream>
template <typename T> class config;
template <typename T> std::ostream& operator<<(std::ostream &out,
const config<T> &c);
template <typename T> class config
{
T *attribute1, *attribute2, *attribute3, *attribute4;
std::string attribName1, attribName2, attribName3, attribName4;
public:
config(void)
{
attribute1 = new T[3];
attribute2 = new T[3];
attribute3 = new T[3];
attribute4 = new T[3];
}
~config(void)//destructor
{
delete [] attribute1;
delete [] attribute2;
delete [] attribute3;
delete [] attribute4;
}
//operator
friend std::ostream& operator<< <T>(std::ostream &out,
const config &c);
};
template <typename T> std::ostream& operator<<(std::ostream &out,
const config<T> &c)
{
for(int i=0;i<3;i++)
{
out << c.attribute1[i] << " "
<< c.attribute2[i] << " "
<< c.attribute3[i] << " "
<< c.attribute2[i] << std::endl;
}
return out;
}
int main()
{
config<double> x;
std::cout << x << std::endl;
}
输出:
0 00 0 0 0 0 0 0 0 0 0
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- ostream过载时的缓冲区冲洗
- gcc和c++17的过载解析失败
- C++Cast运算符过载
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- <<操作员在下面的行中工作
- 过载'operator new'如何导致无限循环?
- "Inverse SFINAE"避免模棱两可的过载
- 操作员C++的模棱两可的过载
- C++中>>操作员过载时出现问题?
- 基于返回类型的转换和过载扣除
- 没有可行的过载'='错误,可能导致这种情况的原因是什么?
- 是否存在包含负号的isdigit函数(过载)
- 过载输出<<用于类的运算符,以打印其中的元组
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- Qt过载数据流运算符<<
- c++11的特定过载<<操作人员
- 过载<<比如cout错误