- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在我的模板类中专门针对 char 的 << 运算符
生命周期
template<class T>
class tablicowy{
public:
T * tablica;
int rozmiar;
public:
tablicowy(T arr[], int n){
{
tablica = arr;
rozmiar = n;
}
};
friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
friend std::ostream& operator<<(std::ostream& out, tablicowy<T>& that ){
out << "( ";
for(int i = 0; i < that.rozmiar; i++){
out << that.tablica[i] << comma;
}
out << ")";
return out;
};
};
cpp
std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ){
out << "'";
for(int i = 0; i < that.rozmiar; i++){
out << that.tablica[i];
}
out << "'";
return out;
};
C++ 给我:
In file included from /home/pawel/ClionProjects/lista9/obliczenia.cpp:1:0: /home/pawel/ClionProjects/lista9/obliczenia.hpp: In instantiation of ‘class obliczenia::tablicowy’: /home/pawel/ClionProjects/lista9/obliczenia.cpp:38:28: required from here /home/pawel/ClionProjects/lista9/obliczenia.hpp:40:30: error: redefinition of ‘std::ostream& obliczenia::operator<<(std::ostream&, obliczenia::tablicowy&)’ friend std::ostream& operator<<(std::ostream& out, tablicowy& that ){ ^ /home/pawel/ClionProjects/lista9/obliczenia.cpp:36:15: error: ‘std::ostream& obliczenia::operator<<(std::ostream&, obliczenia::tablicowy&)’ previously defined here std::ostream& operator<<(std::ostream& out, tablicowy& that ){
我可以做些什么来为 char 重载或专门化该运算符?
最佳答案
您可以使用以下内容:
// Forward declare the class
template <typename T> class tablicowy;
// Forward declare the template operator
template <typename T>
std::ostream& operator<<(std::ostream& out, tablicowy<T>& that );
// Forward declare the function
std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
// Your class:
template<class T>
class tablicowy{
public:
T * tablica;
int rozmiar;
public:
tablicowy(T arr[], int n){
{
tablica = arr;
rozmiar = n;
}
};
// just declare them friend.
friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
friend std::ostream& operator<< <>(std::ostream& out, tablicowy<T>& that );
};
// Implementation
template <typename T>
std::ostream& operator<<(std::ostream& out, tablicowy<T>& that )
{
const std::string comma = ",";
out << "( ";
for(int i = 0; i < that.rozmiar; i++){
out << that.tablica[i] << comma;
}
out << ")";
return out;
}
在 cpp 中:
std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ){
out << "'";
for(int i = 0; i < that.rozmiar; i++){
out << that.tablica[i];
}
out << "'";
return out;
}
关于c++ - 在先前定义的模板类中专门化 friend 运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192508/
我的应用将 SceneKit 内容的“页面”与图像和文本交替。当我从图像页面前进到新的 SceneKit 页面时,前一个 SceneKit 页面中的内容会短暂显示,然后被新内容替换。时髦。 我只使用一
我正在尝试处理(在 C# 中)包含一些数字数据的大型数据文件。给定一个整数数组,如何对其进行拆分/分组,以便如果下一个 n(两个或更多)是负数,则前一个 n 元素被分组。例如,在下面的数组中,应该使用
刚接触promises,研究过。所以我的代码和我的理解: sql.connect(config).then(function(connection) { return connection.req
目前我在 if (roobaf) block 中有一些代码,这取决于 foo 和 bar 是否为假。我可以在 block 内再次检查这些条件,但感觉像是不必要的代码重复。 if (foo) {
我是一名优秀的程序员,十分优秀!