- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码在 Visual Studio 2015 中编译没有问题,但在 minGW 中会出现如下所示的警告和错误:
#include <iostream>
using std::ostream;
template<typename ElemType, int SIZE>
class Array
{
friend ostream &operator<<(ostream &out, const Array<ElemType, SIZE> &value);
ElemType operator[](int index) const;
private:
ElemType elements[SIZE];
};
template<typename ElemType, int SIZE>
ostream &operator<<(ostream &out, const Array<ElemType, SIZE> &value);
template<typename ElemType, int SIZE>
ostream &operator<<(ostream &out, const Array<ElemType, SIZE> &value)
{
out << elements[0];
return out;
}
mingw32-g++.exe -Wall -g -pedantic-errors -pedantic -Wextra -Wall -std=c++98 -c Test.cpp
Test.cpp:7:79: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Array<ElemType, SIZE>&)' declares a non-template function [-Wnon-template-friend]
Test.cpp:7:79: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
Test.cpp: In function 'std::ostream& operator<<(std::ostream&, const Array<ElemType, SIZE>&)':
Test.cpp:21:11: error: 'elements' was not declared in this scope
我在这方面远不是专家,所以我不确定问题出在哪里。它似乎告诉我它需要在类本身的 friend 声明之前添加以下代码,但是当我把它放在那里时它会导致其他编译错误:
template<typename ElemType, int SIZE>
提前致谢!
按照@Trevor Hickey 在其下方的帖子中提出的更改建议进行更改后,关于 friend 模板功能的警告消失了。但是,我仍然收到有关未在范围内声明“元素”(在友元函数中)的错误。
最佳答案
您的代码中存在两个不同的问题。比较简单的是 out << elements[0];
应该是 out << value.elements[0];
.这是因为您要打印属于参数 value
的元素。 .请记住,我们在这里处于非成员函数中,没有 this
并且没有您可以通过非限定名称访问的成员。
另一个被称为 template friends problem .到目前为止你只得到一个警告,但是如果你试图编译一个完整的程序你会得到一个错误。我添加了代码:
int main() { Array<int, 5> a; std::cout << a; }
然后出现错误:
undefined reference to `std::ostream& operator<< <int, 5>(std::ostream&, Array<int, 5> const&)'
问题是你的代码:
friend ostream &operator<<(ostream &out, const Array<ElemType, SIZE> &value);
实际上声明了一个非模板 函数,它将成为Array
实例的友元。 .稍后,当你写 cout << a
在main
,编译器匹配 <<
到这个非模板声明。它永远不会达到 operator<<
的正文您稍后提供的,因此永远不会实例化该主体的拷贝,因此出现 undefined reference 错误。
解决此问题的一种方法是显式实例化主体。但这很蹩脚,因为您必须为 Array
的每个可能实例化编写一个显式实例化。发生在您的代码中。所以我们不会这样做。
最简单的解决方案是将 operator<<
的主体放在在类定义中内联。
另一种选择是声明 operator<<
是模板函数。
Trevor Hickey 的代码展示了一种方法,尽管它有一个缺点 Array<A, B>::elements
可以通过 Array<C, D>::operator<<
访问.
更安全的方法是声明 operator<<
课前:
template<typename ElemType, int SIZE>
class Array;
template<typename ElemType, int SIZE>
ostream &operator<<(ostream &out, const Array<ElemType, SIZE> &value);
然后是你之前的其余代码。现在,friend
类中的声明将匹配预先存在的模板,而不是声明一个新的非模板。
关于C++ friend 模板函数 - minGW 错误但不是 VS2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36974166/
我想知道将来对我来说最简单的方法是什么,可以使查询既有效又不那么复杂。 我应该像这样保存双向关系吗 from_id=1, to_id=2from_id=2, to_id=1 或者只创建一个唯一的行 f
假设您有一个拥有十亿用户的社交网络。在每个用户的页面上,您想要显示该用户的 friend 数、 friend 的 friend 等等,最多五度。友谊是相互的。计数不需要立即更新,但它们应该是精确的。
public void notMyFriend(Student student1) { System.out.println("Friends who are not my frien
我有如下3个表格 用户 - id integer primary_key - user_name friend - id autoincrement primary_key - user1 integ
如何从 Friends 表中获取 friend 列表以及我 friend 的 friend 数(不包括我的 friend 数) friend 表"tbl_users_friends 字段 1:id字段
我的 MySQL 表结构是这样的。 USER int id varchar username FRIEND_LIST int user_id int friend_id 对于每个 friend 关系,
假设 a,b,c 和 b,d,e 和 c,f,g 和 a,e,g,h 是 friend 。所以 获取共同好友 b/w a&b: MATCH (me:User {username:'a'}) -[r:F
我正在开发“可能的 friend ”功能。我需要显示所有不是我 friend 的 friend 的 friend ,也不要发送我或没有我的待处理请求 FRIENDSHIPS user_id frien
如果两个用户是 friend ,我在“ friend ”表中有一个条目,如下所示: table: friends ------------------------ uid1
我有一个 friend 表,用于跟踪数据库中的关系 - 我将一个查询与子查询放在一起,该子查询从 friend 的 friend 那里获取个人资料信息 我的 friend 表如下所示 - id 双向出
我想结交 friend 的 friend 的 friend 。我得到了结果,但我不确定我的查询是否正确。我正在使用嵌套选择。 我的问题是: 查询是否正确? 我如何使用 join 执行此查询? 这是我的
假设我有一个包含 friend 属性的用户对象。这个 friends 属性是其他用户对象的数组,因此是你的 friend 。找到你 friend 的 friend 但不是你 friend 的最快算法是
我想结交我的 friend 以及 friend 的 friend ,但仅限于二级 friend 我的表结构是这样的 user_id friend_user_id 1 2 1
为了了解使用 Neo4J 建立 friend 关系的优势,我在 MySQL 数据库上创建了一张用于 Persons 的表(“Persons”,20900 个数据集): id | name --
假设我有一个名为“ friend ”的表,对于每个友谊,我添加两个条目。例如,如果用户 1 和 2 是 friend ,我们将有: uid1 uid2 ---------- 1 2 2 1
在我的图形数据库中,我有 Twig 和树叶。分支可以“包含”叶子,分支可以“包含”分支。 如何使用 Gremlin 找到与给定分支直接或间接相关的所有叶子? 我让这个在 Cypher 中工作: STA
我正在尝试使用 iPhone Facebook SDK 获取我 friend 的 friend 列表。我尝试了一种使用 FQL 和 Graph API 的方法,但在这两种情况下我都遇到了错误: "Ca
我有这个 MySQL 表: 假设我以用户 1 的身份登录,正在浏览用户 2 的个人资料。由于我们是共同的 friend (1 是 2 的 friend ,2 是 1 的 friend ),我需要回应“
我想得到我 friend 的 friend 不是我 friend 的 friend 。我有一张这样的 table :用户 friend (idUser,idUserFriend) 我在想这样的事情:
我有这个 Cypher 查询... match (p:Person{userid:8432})-[r:friends_with]->(p1:Person)-[r2:friends_with]->(p2
我是一名优秀的程序员,十分优秀!