我试图将全局函数声明为类的"朋友":
namespace first
{
namespace second
{
namespace first
{
class Second
{
template <typename T> friend T ::first::FirstMethod();
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Visual C++ 2008下编译此代码时,我得到:
error C3254: 'first::second::first::Second' : class contains explicit override 'FirstMethod' but does not derive from an interface that contains the function declaration
error C2838: 'FirstMethod' : illegal qualified name in member declaration
Run Code Online (Sandbox Code Playgroud)
如果我使用,template <typename T> friend T first::FirstMethod();我得到:
error C2039: 'FirstMethod' : is not a member of 'first::second::first'
Run Code Online (Sandbox Code Playgroud)
宣布朋友功能的适当方式是什么?
说我有一个这样的课:
class Ingredient
{
public:
friend istream& operator>>(istream& in, Ingredient& target);
friend ostream& operator<<(ostream& out, Ingredient& data);
private:
Measure myMeas;
MyString myIng;
};
Run Code Online (Sandbox Code Playgroud)
在这个重载的朋友函数中,我正在尝试设置值 myIng
istream& operator>>(istream& in, Ingredient& target)
{
myIng = MyString("hello");
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这应该工作,因为我在友元函数中设置了Ingredient类的私有数据成员的值,而友元函数应该可以访问所有私有数据成员吗?
但是我得到了这个错误: ‘myIng’ was not declared in this scope
对于为什么会发生这种情况的任何想法?
考虑名称空间内的类。该类的定义声明了一个朋友函数。
namespace Foo
{
class Bar
{
friend void baz();
};
}
Run Code Online (Sandbox Code Playgroud)
根据我所知道的,这应该声明baz()为最内部封闭的命名空间(即)的成员Foo。
因此,我希望以下定义baz()是正确的:
void Foo::baz() { }
Run Code Online (Sandbox Code Playgroud)
但是,GCC(4.7)给我一个错误。
error: ‘void Foo::baz()’ should have been declared inside ‘Foo’
Run Code Online (Sandbox Code Playgroud)
几种解决方案似乎有效:
baz()在课堂外宣布。
namespace Foo
{
void baz();
class Bar
{
friend void baz();
};
}
Run Code Online (Sandbox Code Playgroud)baz()在名称空间内定义。
namespace Foo
{
class Bar
{
friend void baz();
};
}
...
namespace Foo
{
void baz() { }
}
Run Code Online (Sandbox Code Playgroud)使用该-ffriend-injection标志进行编译,从而消除了错误。
这些解决方案似乎与我知道的C ++中声明/定义的一般规则不一致。
为什么我必须申报baz()两次? …
我有一个Foo类,必须在其他类Bar中“直接”访问。我想构建一个小框架,声明Bar方法(这是Foo的朋友方法)受保护的方法。这样,我可以为Bar培养几个班级的孩子。
Gcc抱怨这一点,并且只有在该方法是公开的情况下才起作用。
我能怎么做?我的代码示例:
class Foo;
class Bar {
protected:
float* internal(Foo& f);
};
class Foo {
private:
//some data
public:
//some methods
friend float* Bar::internal(Foo& f);
};
Run Code Online (Sandbox Code Playgroud)
Gcc讯息:
prog.cpp:4:16: error: ‘float* Bar::internal(Foo&)’ is protected
float* internal(Foo& f);
^
prog.cpp:11:43: error: within this context
friend float* Bar::internal(Foo& f);
^
Run Code Online (Sandbox Code Playgroud) 我已经在Turbo-C中编译了第一个版本的代码,它编译时没有任何错误.但是当我在Visual Studio中编译它或从CommandLine编译普通g ++时,我会在帖子中看到错误.
我通过互联网搜索并阅读了一些StackOverflow问题和MSDN LNK1120问题文档.我找到了修复下面代码的方法.但是,没有明确的原因.如何定义摆脱该错误.从语法上讲,容易出错的代码看起来也很好.
1) Error 2 error LNK1120: 1 unresolved externals
2) Error 1 error LNK2019: unresolved external symbol "void __cdecl
totalIncome(class Husband<int> &,class Wife<int> &)" (?totalIncome@@YAXAAV?
$Husband@H@@AAV?$Wife@H@@@Z) referenced in function _main
Run Code Online (Sandbox Code Playgroud)
注意:请注意程序中的箭头.我明确地说过了.所以,可能不会通过完整的程序.
错误易错代码:
#include <iostream>
using namespace std;
template<typename T> class Wife; // Forward declaration of template
template<typename T>
class Husband{
friend void totalIncome(Husband<T> &hobj, Wife<T> &wobj);
public:
Husband() = default;
Husband(T new_salary): salary{new_salary} {}
private:
T salary;
};
template<typename T>
class Wife{
friend void totalIncome(Husband<T> &hobj, …Run Code Online (Sandbox Code Playgroud) 显然今天,MSVC正在尽力说服我切换到铿锵声.但我不会放弃.早些时候,我问这个问题,不知道如何声明std::make_unique作为friend我的班.
我在我的简单场景中得到了一个非常好的答案,当我在wandbox上使用clang尝试它时,它编译得很好.
所以我很高兴回到Visual Studio 2013继续编码.我的代码的一部分是这样的:
// other includes
#include <string>
#include <memory>
template <typename Loader, typename Painter, typename MeshT>
class Model
{
public:
friend std::unique_ptr<Model> std::make_unique<Model>(
const std::string&,
const std::shared_ptr<Loader>&,
const std::shared_ptr<Painter>&);
// Named constructor
static std::unique_ptr<Model> CreateModel(
const std::string& filepath,
const std::shared_ptr<Loader>& loader,
const std::shared_ptr<Painter>& painter)
{
// In case of error longer than the Lord of the Rings trilogy, use the
// line below instead of std::make_unique
//return std::unique_ptr<Model>(new Model(filepath, …Run Code Online (Sandbox Code Playgroud) 我有一个课程entry并且被ostream& operator <<覆盖了.我还有一个辅助类cursor和一个类型转换operator entry().然后,在我的main()函数中,我有以下表达式:
cout << data[4];
Run Code Online (Sandbox Code Playgroud)
这里data[4]是一个cursor,但编译失败
错误:二进制表达式的操作数无效
我想要的是编译器转换data[4]为entry和使用它的<<运算符.有没有办法以上述方式调用此ostream运算符而无需添加特殊方法entry?
以下是一些代码:
class entry
{
friend class cursor;
/*here comes some data*/
public:
friend ostream& operator << (ostream& out, const entry& a);
};
Run Code Online (Sandbox Code Playgroud)
class cursor
{
database* data_;
size_t ind_;
friend class entry;
friend class database;
public:
cursor (database* a, size_t ind);
cursor (const cursor& a);
void operator= (const …Run Code Online (Sandbox Code Playgroud) c++ operator-overloading friend-function argument-dependent-lookup
我有三个文件:matrix.cpp main.cpp matrix.hpp.在matrix.hpp中声明了一个函数str_matrix(const char*),它的文本在matrix.cpp中.在main.cpp中我想使用这个函数.在matrix.hpp中:
class matrix {
...
friend matrix str_matrix(const char*);
Run Code Online (Sandbox Code Playgroud)
在matrix.cpp中:
#include "matrix.hpp"
#include <iostream>
#include <cstdlib>
#include <cstdio>
...
matrix str_matrix(const char* a) {
...}
Run Code Online (Sandbox Code Playgroud)
在main.cpp中:
#include <iostream>
#include <fstream>
#include "matrix.hpp"
#include <cstdlib>
#include <cstdio>
...
matrix m1(str_matrix ("{1}"));
Run Code Online (Sandbox Code Playgroud)
但我有一个错误:
nirvana@lpt00:~/cpp/matrix$ g++ main.cpp matrix.cpp
main.cpp: In function ‘int main()’:
main.cpp:12:30: error: ‘str_matrix’ was not declared in this scope
matrix m1(str_matrix ("{1}"));
^
Run Code Online (Sandbox Code Playgroud)
我该怎么办呢?
我尝试重载operator<<,但有警告我无法重载。我可以按如下方式重载该运算符:
std::cout << test4 << std::endl;
Run Code Online (Sandbox Code Playgroud)
但是我不能按如下方式重载它:
std::cout << test2 + test3 << std::endl;
Run Code Online (Sandbox Code Playgroud)
我的主要代码是:
Stonewt test2(2, 8, Stonewt::STONE);
std::cout << "test2: " << test2 << std::endl;
Stonewt test3(2, 3, Stonewt::POUNDS);
std::cout << "test3: " << test3 << std::endl;
Stonewt test4 = test2 + test3;
std::cout << test4 << std::endl; // operator << can overload
std::cout << test2 + test3 << std::endl; // operator << cannot overload
Run Code Online (Sandbox Code Playgroud)
下面是friend功能
std::ostream& operator <<(std::ostream& os, Stonewt& a)
{
if …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我试图创建一个函数"patient_count",它是"horse","pig"和"dog"类的朋友.我可以让这个功能成为一个班级的朋友而不是所有的3个人.任何人都能告诉我我的错误是什么吗?
/*******************************************************\
* Veternarian Class Problem - I need a class for each *
* of 3 animals. Horse, Pig and Dogs *
\*******************************************************/
#include <cstdlib>
#include <iostream>
#include <string>
const int HORSE_KENNEL = 100; // Number of horses we can store
const int PIG_KENNEL = 100; // Number of Pigs we can store
const int DOG_KENNEL = 100; // Number of Dogs we can store
/*******************************************************\
* Class horse *
* *
* Member functions *
* horse_count -- …Run Code Online (Sandbox Code Playgroud) c++ ×10
friend-function ×10
class ×2
c++11 ×1
c++14 ×1
msvc12 ×1
namespaces ×1
templates ×1
unique-ptr ×1