Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用c+中的类模板声明新运算符+;_C++_Templates_Operator Overloading - Fatal编程技术网

C++ 如何使用c+中的类模板声明新运算符+;

C++ 如何使用c+中的类模板声明新运算符+;,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我有一个模板类(Word): 模板 类词{ ... } 我想添加一个操作符: friend Word<C> operator&(Word<C>& word1, Word<C>& word2); // & operator between all bits undefined reference to `operator&(Word<8>&, Word<8>&)' frie

我有一个模板类(Word):

模板
类词{
...
}
我想添加一个操作符:

friend Word<C> operator&(Word<C>& word1, Word<C>& word2);   // & operator between all bits
undefined reference to `operator&(Word<8>&, Word<8>&)'
friend-Word操作符&(Word&word1,Word&word2);//&所有位之间的运算符
所以现在我有:

template<int C>
class Word {
   ...
   friend Word<C> operator&(Word<C>& word1, Word<C>& word2);    // & operator between all bits
}

template<int C>
Word<C> operator&(Word<C>& word1, Word<C>& word2) {
    Word<C> ans = new Word<C>;

    for (int i = 0; i < C; i++) {
        ans[i] = word1[i] & word2[i];
    }
    return ans;
}
模板
类词{
...
所有位之间的友元字运算符&(字和字1,字和字2);//&运算符
}
模板
单词运算符&(单词和单词1,单词和单词2){
单词ans=新单词;
对于(int i=0;i
但我有这样的想法:

Multiple markers at this line
    - (if this is not what you intended, make sure the function template has already been declared and add <> after the function name 
     here)
    - friend declaration 'Word<C> operator&(Word<C>&, Word<C>&)' declares a non-template function [-Wnon-template-friend]
此行有多个标记
-(如果这不是您想要的,请确保函数模板已经声明并添加到函数名称之后。)
这里)
-友元声明'Word运算符&(Word&,Word&)'声明一个非模板函数[-Wnon-template-friend]
使用运算符时出现此错误:

friend Word<C> operator&(Word<C>& word1, Word<C>& word2);   // & operator between all bits
undefined reference to `operator&(Word<8>&, Word<8>&)'
operator&(Word&,Word&)的未定义引用
使用代码:

Word<C> tmp1 = (words[i] & mask);
wordtmp1=(words[i]和mask);

先声明函数,然后再将其作为好友

// Forward declare the class.
template<int C> class Word;

// Declare the function.
template<int C> Word<C> operator&(Word<C>& word1, Word<C>& word2);
//向前声明类。
模板类词;
//声明函数。
模板字运算符&(字和字1,字和字2);

然后,类中的
friend
声明应该是正常的。

问题是由于您没有声明成员
运算符&
重载,因此结果声明没有模板化。如果使用
g++
编译,您会看到一条与此相关的有用警告消息:

test4.cpp:4:60: warning: friend declaration ‘Word<C> operator&(Word<C>&, Word<C>&)’ declares a non-template function [-Wnon-template-friend]
test4.cpp:4:60: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
test4.cpp: In function ‘int main()’:
编译并运行:

$ g++ test4.cpp && ./a.out
Inside operator &
8

你在哪里申报单词[i]和面具?此外,运算符实现中存在内存泄漏。返回一个指向单词的指针,但返回类型为Word。编译吗?
wordans=新单词应该是
单词ans