我有重载operator <<为模板类的问题.我使用的是Visual Studio 2010,这是我的代码.
#ifndef _FINITEFIELD
#define _FINITEFIELD
#include<iostream>
namespace Polyff{
template <class T, T& n> class FiniteField;
template <class T, T& n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);
template <class T, T& n> class FiniteField {
public:
//some other functions
private:
friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
T _val;
};
template <class T, T& n>
std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
return out<<f._val;
}
//some other definitions
}
#endif
Run Code Online (Sandbox Code Playgroud)
在主要我只是
#include"FiniteField.h"
#include"Integer.h"
#include<iostream>
using std::cout;
using …Run Code Online (Sandbox Code Playgroud) c++ templates operator-overloading visual-studio-2010 friend-function
下面的示例编译良好,但我无法弄清楚如何在这种特殊情况下分离运算符 <<() 的声明和定义。
每次我尝试拆分定义时,朋友都会造成麻烦,并且 gcc 抱怨运算符 <<() 定义必须恰好采用一个参数。
#include <iostream>
template <typename T>
class Test {
public:
Test(const T& value) : value_(value) {}
template <typename STREAM>
friend STREAM& operator<<(STREAM& os, const Test<T>& rhs) {
os << rhs.value_;
return os;
}
private:
T value_;
};
int main() {
std::cout << Test<int>(5) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
Operator<<() 应该有一个自由的第一个参数来处理不同类型的输出流(std::cout、std::wcout 或 boost::asio::ip::tcp::iostream)。第二个参数应该绑定到周围类的专门版本。
Test<int> x;
some_other_class y;
std::cout << x; // works
boost::asio::ip::tcp::iostream << x; // works
std::cout << y; // doesn't work
boost::asio::ip::tcp::iostream << y; …Run Code Online (Sandbox Code Playgroud) #include <iostream>
class B;
class A{
int a;
public:
friend void B::frndA();
};
class B{
int b;
public:
void frndA();
};
void B::frndA(){
A obj;
std::cout << "A.a = " << obj.a << std::endl;
}
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试编译此代码时,发生了一些错误.例如
无效使用不完整类型
这段代码有什么问题?
定义operator "" (...)作为朋友函数是否可行和/或有用?
class Puzzle {
friend Puzzle operator "" _puzzle(const char*, size_t);
...
};
void solve(Puzzle);
int main() {
solve("oxo,xox"_puzzle);
};
Run Code Online (Sandbox Code Playgroud)
我正在考虑"有用",因为规则只operator ""应在命名空间中定义 - 至少是因为_以全局命名空间中保留的标识符开头.这friend打破了这条规则吗?所以,这种不完全封装没有任何好处,对吧?
这让我感到惊讶.这有效:
struct foo {
int x;
friend int x(foo f) { return f.x; }
friend int y(foo f);
};
int y(foo f) { return x(f); } // no problem
Run Code Online (Sandbox Code Playgroud)
但这是一个错误:
struct foo {
int x;
friend int x(foo f) { return f.x; }
friend int y(foo f) { return x(f); } // error: invalid use of foo::x data member
};
Run Code Online (Sandbox Code Playgroud)
为什么不允许这两种(dis)?
我有以下模板类和模板函数,它们打算访问类的私有数据成员:
#include <iostream>
template<class T>
class MyVar
{
int x;
};
template<class T>
void printVar(const MyVar<T>& var)
{
std::cout << var.x << std::endl;
}
template<class T>
void scanVar(MyVar<T>& var)
{
std::cin >> var.x;
}
struct Foo {};
int main(void)
{
MyVar<Foo> a;
scanVar(a);
printVar(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为了将这两个函数声明为MyVar<T>友元函数,我在声明中尝试了以下方法template<class T> class MyVar来声明友元。它们都不起作用。我应该怎么做?
template<class T> friend void printVar(const MyVar&);
template<class T> friend void scanVar(MyVar&);
// compilation error
template<class T> friend void printVar(const MyVar<T>&);
template<class T> friend void scanVar(MyVar<T>&); …Run Code Online (Sandbox Code Playgroud) 在给定的c ++代码中,类DEF的私有成员在构造函数中初始化,并再次在友元函数内部进行初始化.所以重新定义会覆盖私有变量,否则构造函数给出的值会持续存在?
#include<iostream>
//class DEF;
class ABC{
public:
int fun(class DEF);
};
class DEF{
private:
int a,b,c;
public:
DEF():a(1),b(12),c(2){}
friend int ABC::fun(class DEF);/*Using friend function to access the private member of other class.*/
void fun_2();
};
void DEF::fun_2(){
cout<<"a : "<<&a<<' '<<"b : "<<&b<<' '<<"c: "<<&c<<endl;
cout<<"a : "<<a<<' '<<"b : "<<b<<' '<<"c: "<<c<<endl;
}
int ABC::fun(class DEF A){
A.a = 10;
A.b = 20;
A.c = 30;
int data = A.a + A.b + A.c;
cout<<"a : "<<&(A.a)<<' '<<"b …Run Code Online (Sandbox Code Playgroud) 根据标准,在类中声明和定义的友元函数只能由ADL查找.所以,我认为以下代码应该编译.
template<int M>
struct test{
template<int N = 0>
friend void foo(test){}
};
int main(){
test<2> t;
foo(t);// compile
foo<1>(t);// error
}
Run Code Online (Sandbox Code Playgroud)
但是,gcc给出以下错误:
main.cpp: In function 'int main()':
main.cpp:10:5: error: 'foo' was not declared in this scope
foo<1>(t);
^~~
Run Code Online (Sandbox Code Playgroud)
然后,我有三个问题.
template<int N> foo按照标准找到?foo发现foo<1>不是?foo外部之外还有解决方法吗?c++ friend-function function-templates argument-dependent-lookup
我有一个结构模板A<x>和一个+运算符int.
#include <iostream>
template<int x>
struct A{
int a;
};
template<int x>
int operator+(A<x> a, int b){
return a.a+b;
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个B<x>可转换为的结构模板A<x>.
template<int x>
struct B{
int b=3;
operator A<x>(){
return {b+10};
}
};
Run Code Online (Sandbox Code Playgroud)
现在我希望在打电话时B<x>转换成.A<x>B<x> + int
int main(){
std::cout<<(A<12>{9}+10)<<std::endl;//OK
std::cout<<(B<12>{9}+10)<<std::endl;//Error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在重载模板类的运算符时读取了隐式转换并写了
template<int x>
struct B{
int b=3;
operator A<x>(){
return {b+10};
}
friend int operator+(A<x> a, int b);
}; …Run Code Online (Sandbox Code Playgroud) 当派生类通过公共访问从基类继承时,问题与友元函数是否被继承?为什么基类 FRIEND 函数可以在派生类对象上工作?。但是,如果它通过受保护或私有访问继承,则会出现可见性错误。
当它通过公共访问继承时,A 的私有成员的可访问性与通过私有访问继承时相同。他们之间有什么区别?
class A {
private:
int a;
friend void f();
};
class B : private A {
};
void f() {
B obj;
int x = obj.a;
}
int main() {
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×10
friend-function ×10
templates ×4
friend ×3
c++11 ×1
c++14 ×1
class ×1
inheritance ×1
inline ×1
member ×1