- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前问过这个问题here ,却得不到答复,只是“绕道而行”。现在,我正在尝试找到问题的实际解决方案(如下所述)。在有人说之前有人问过这个问题之前,我想说我尝试了提供的解决方案 here , here , here , 和 here - 没有任何帮助:(
问题是链接器说 Undefined symbols for architecture x86_64
没有任何其他警告或错误。调用、完整错误消息和正在编译的代码如下所示。
注意:如果我定义 operator<<
inline,问题就消失了,但这并不是真正的解决方案,而是绕了个弯路:)
提前谢谢你:)
环境:
uname -a
: Darwin wireless1x-XXX-XXX-XXX-XXX.bu.edu 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_6
g++ --version
: Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
运行参数:
g++ -std=c++11 -lm -stdlib=libc++ tstLinkedList1.cpp -o tstLinkedList1
或
g++ -std=c++11 -lm -stdlib=libstdc++ tstLinkedList1.cpp -o tstLinkedList1
我还尝试添加 -lc++
在这两种情况下 - 同样的事情 :(
编辑错误发生在operator<<
重载,定义在 LinkedList.hpp
的最后下面的文件
使用 -stdlib=libc++
:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, LinkedList<int> const&)", referenced from:
_main in tstLinkedList1-66598f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
使用 -stdlib=libstdc++
:
Undefined symbols for architecture x86_64:
"operator<<(std::ostream&, LinkedList<int> const&)", referenced from:
_main in tstLinkedList1-8d9300.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
链表.hpp:
#pragma once
template <typename T> class LinkedList;
template <typename T>
std::ostream& operator<<(std::ostream& os, const LinkedList<T>& list);
/** Node class
*
* @tparam T template type
*/
template <typename T>
class Node {
private:
T _elem; //!< Stored value
Node<T>* _next; //!< Next element
friend class LinkedList<T>; //!< Friend class
};
/** Singly Linked List
*
* @tparam T template type
*/
template <typename T>
class LinkedList {
public:
LinkedList();
~LinkedList();
std::size_t size() const;
bool empty() const;
const T& front() const;
void addFront(const T& e);
void removeFront();
public:
// Housekeeping
friend std::ostream& operator<<(std::ostream& os, const LinkedList<T>& list);
private:
Node<T>* _head;
std::size_t _size;
};
/** Constructor */
template <typename T>
LinkedList<T>::LinkedList() : _head(nullptr), _size(0) {}
/** Destructor */
template <typename T>
LinkedList<T>::~LinkedList() {
while (!empty()) removeFront();
}
/** Number of elements in the list
*
* @returns std::size_t Number of elements in the list
*/
template <typename T>
std::size_t LinkedList<T>::size() const {
return this->_size;
}
/** Empty?
*
* @returns bool True if empty
*/
template <typename T>
bool LinkedList<T>::empty() const {
return _head == nullptr;
}
/** Get front element (read-only)
*
* @returns T
*/
template <typename T>
const T& LinkedList<T>::front() const {
return _head->_elem;
}
/** Add element in the front of the list
*
* @param e Element to be added
*/
template <typename T>
void LinkedList<T>::addFront(const T& e) {
Node<T>* v = new Node<T>;
v->_elem = e;
v->_next = _head;
_head = v;
_size++;
}
/** Remove the first element */
template <typename T>
void LinkedList<T>::removeFront() {
if (empty()) return;
Node<T>* old = _head;
_head = old->_next;
_size--;
delete old;
}
/** Operator<< for the linked list
*
* @returns std::ostream
* @param LHS->std::ostream
* @param RHS->LinkedList<T>
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const LinkedList<T>& list) {
os << "TEST";
return os;
}
tstLinkedList1.cpp:
#include <iostream>
#include "LinkedList.hpp"
using namespace std;
int main() {
LinkedList<int> ll1;
ll1.removeFront();
ll1.addFront(1);
std::cout << ll1 << std::endl;
}
最佳答案
friend 声明并没有按照您的想法行事。它声明了一个非模板函数,而不是您之前声明的模板。你需要的是:
// within LinkedList:
template<typename U>
friend std::ostream& operator<<(std::ostream&, const LinkedList<U>&);
匹配您声明的模板并使那个成为 friend 。嗯,整个模板。或者,您也可以使用
// within LinkedList:
friend std::ostream& operator<<<>(std::ostream&, const LinkedList&);
// NOTE: ^^ here you need to add <>
给 friend 一个T
(您可以使用 LinkedList
而不是 LinkedList<T>
- 在类里面没有区别)。
如果你只使用
// within LinkedList:
friend std::ostream& operator<<(std::ostream&, const LinkedList<T>&);
你需要有
// within the global namespace
// NOTE: not a template!
std::ostream& operator<<(std::ostream&, const LinkedList<int>&);
使您的示例工作。或者你可以定义 friend operator<<
内联:
// within LinkedList:
friend std::ostream& operator<<(std::ostream& os, const LinkedList<T>& list)
{
// implement me!
return os;
}
并完全删除前向声明。
关于c++ - g++ : linker issue on Mac OS X - Undefined symbols for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149807/
我不知道我做了什么来破坏我的 QtCreator,但是“突然”我所有的编译器问题(警告/错误)不再显示在问题面板中,而是只显示在“编译输出”面板中(我在那里不能使用它们通过单击跳转到错误代码)。 在“
我正在制作游戏作为自学。 我的目标是在鼠标指针后面有一条轨迹。(这有效)但是,当我在 chrome 上打开并运行它时。在达到一定的高度和宽度后,轨迹保留在 Canvas 上而不是被擦掉。谁能帮我解决这
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我一直在尝试将背景图像添加到 html 文档中元素的边框: 你可以在这里看到:HTML: Something CSS: a { border-width:10px; border-co
我即将开始一个连接两个程序的项目,一个用c#,一个用c++。我已经有一个可以工作的 c# 程序,它能够与自身的其他版本对话。在开始使用c++版本之前,我想到了一些问题: 1) 我正在使用 protob
我正在尝试线程优先级的影响,当 run 方法中的 println 保留在注释中时,两个线程同时结束,我不理解这种行为,你能解释一下吗?谢谢。 主类 public class Main { public
jQuery 1.4: $('#menu li:last:not(.ignore)').addClass('last'); HTML: item 1 ite
当我创建或查看问题时,我想从Mantis问题跟踪系统中删除选择配置文件及其所有随附字段,因为这些问题与我要创建的任何问题都不相关。 其中包括配置文件部分随附的“平台”,“操作系统”和“操作系统版本”字
我不在Master分支上工作。我正在开发另一种newFeature,该功能也发布在github上。 我知道在处理Master分支时如何解决问题:Closes #XXX。 但是,这仅在我在Master分
我需要在一些UICollectionViewCell中画一个圆圈。具有不同颜色边框和背景颜色的圆圈。我的代码。 UICollectionViewController - (UICollectionVi
我们将Trac用于我正在从事的开源项目。我想将所有公开票导出到Github的Issues。我已经找到了一些可能的小型脚本,但是我想知道是否有人这样做并提出了建议。 理想情况下,它将trac的描述语法转
我对 iOS 编程还比较陌生,但在其他语言(主要是 C# 和 Java)方面拥有丰富的编程经验。 我正在尝试为 iPad 设备的某些产品制作一个类似图片库的东西,其中包含指定的类别。用户界面非常简单,
我已经在中间人中激活了 I18n,如下所示: activate :i18n, mount_at_root: :de 现在我想自动从 / 重定向到 /de。这可能吗? 另外,我想知道为什么中间人使用 p
我已经成功编写了一个程序,可以从 Excel 文件中读取数据并将它们存储在 mysql 的表中。我的程序读取每个文件的第一行,创建表的字段,并将其余数据存储为每列中的值。正是因为这种情况以编程方式发生
这是我的代码。 function m1() { return new Promise(function(resolve, reject) { var files = $('#aws-upl
谁能告诉我这是怎么回事? #include #include #include #include #include class writeManager { std::vector
compile 'com.github.RogaLabs:social-login:1.2.1' compile 'com.github.mukeshsolanki:social-login-help
我有一个巨大的 csv 文件,其中包含 149 列 和 25K+ 行,可以在 MySQL 中上传该文件> 我正在使用MySQL LOAD DATA 查询 表我的查询是: LOAD DATA local
我是一名优秀的程序员,十分优秀!