我在尝试编译一个C++模板类时遇到错误,该类在一个.hpp和.cpp文件之间分开:
$ g++ -c -o main.o main.cpp
$ g++ -c -o stack.o stack.cpp
$ g++ -o main main.o stack.o
main.o: In function `main':
main.cpp:(.text+0xe): undefined reference to 'stack<int>::stack()'
main.cpp:(.text+0x1c): undefined reference to 'stack<int>::~stack()'
collect2: ld returned 1 exit status
make: *** [program] Error 1
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
stack.hpp:
#ifndef _STACK_HPP
#define _STACK_HPP
template <typename Type>
class stack {
public:
stack();
~stack();
};
#endif
Run Code Online (Sandbox Code Playgroud)
stack.cpp:
#include <iostream>
#include "stack.hpp"
template <typename Type> stack<Type>::stack() {
std::cerr << "Hello, …Run Code Online (Sandbox Code Playgroud) 另一个简单例子:
if (wpa_s->mlme.ssid_len == 0)
return -EINVAL;
Run Code Online (Sandbox Code Playgroud)
为什么一元减去?对于在成功时返回> 0且在失败时返回<(=)0的函数,这(通常)是否完成,还是有其他原因?
我正在创建一个显示带有一些文本的窗口的类,一个"不再显示"复选框和一个按钮.为了可重用,类会根据需要调整窗口大小以适合文本.
但是,在计算中有一些细微的不精确 - 如果我不向宽度添加5个像素,则某些字符串会被截断.(简单地删除了最后一个字.)
这就是我所拥有的:
// NSTextField *textLabel;
// NSString *text;
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject: [textLabel font] forKey: NSFontAttributeName];
NSRect textFrame = [text boundingRectWithSize:NSMakeSize(textLabel.frame.size.width, (unsigned int)-1)
options:(NSStringDrawingDisableScreenFontSubstitution | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:stringAttributes];
textFrame.size.width += 5;
Run Code Online (Sandbox Code Playgroud)
我暂时将标签的背景颜色设置为黄色以使调试更容易,并且它显然扩展到几乎适合最后一个单词.该测试字符串上有4个像素的额外值.
请注意,没有添加这些像素,并非所有字符串都会失败
我关心的原因有两个:
1)我想知道为什么它有点错误,更重要的是
2)我想通过改变计算后的宽度,理论上包装可以改变并且不使用最后一行,在文本下面创建额外的空白区域.
好的,所以我现在有两个(完全不相关的,不同的项目)类使用迭代器.一个拥有iterator并按reverse_iterator预期工作,另一个,当前一个具有iterator半破坏const_iterator(具体而言,因为const_iterator派生自迭代器,代码LinkedList<int>::iterator i = const_list.begin()有效并允许您修改const定义的列表......).
我打算将所有四种类型添加到这个类......如果可以的话.
我如何继续最小化复制/粘贴代码并仅更改返回类型?创建一个类似于base_iterator继承的基类?创建一个iterator或const_iterator从中继承?从某些std :: class继承?如果这些案例中的任何一种都是"最佳"方法,那么代码会在哪里?
也许没有其他选择是好的?我在这里迷路了,找不到太多的参考资料.
任何建议都表示赞赏,但请记住,我是这个主题的新手(一般都是迭代器和C++,尤其是OOP).我试图研究GCC附带的头文件是徒劳的 - 它们并不完全是我正在寻找的教程.
在尝试为我的链表类添加迭代器支持时,我从g ++中收到以下错误.
LinkedList.hpp: In member function ‘Type& exscape::LinkedList<Type>::iterator::operator*() [with Type = int]’: tests.cpp:51: instantiated from here LinkedList.hpp:412: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘exscape::LinkedList<int>::iterator*’
可能相关的代码片段:
LinkedList.hpp:
template <typename Type>
class LinkedList {
private:
struct node {
struct node *prev;
struct node *next;
Type data;
};
public:
class iterator : public std::iterator<...> {
node *p;
public:
Type &operator*();
};
...
};
template <typename Type>
LinkedList<Type>::iterator::iterator(struct node *in_node) : p(in_node) {}
template <typename Type> …Run Code Online (Sandbox Code Playgroud) 我正在尝试在C/x86_64内联汇编中编写一个函数,但我没有太多运气.
我们可以把它归结为:
void f(const unsigned char *a, unsigned char *b, const unsigned char *c) {
asm __volatile__ ("movdqa %0, %%xmm0" : : "m"(a) : "%xmm0");
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一下,该函数被正确调用;我正在尝试替换C代码,当我使用现在注释掉的C代码时,它的工作正常.)
这崩溃了:
异常类型:EXC_BAD_ACCESS(SIGSEGV)
异常代码:0x000000000000000d,0x0000000000000000
崩溃的线程:0调度队列:com.apple.main-thread
我已经尝试了很多约束的组合,但(正如我所说),我运气不好.
目标是访问程序集中的所有三个函数参数; a只读,b读写和c只读.
如您所见,这三个都是C中的char数组; 然而,这两个a和b16个字节长,每个寄存器可以存储在一个XMM(这是我的目标之一).
c是这样的变量的数组,因此每个变量也可以存储在XMM寄存器中.
另外,我应该指出,我更喜欢GCC没有将内容加载到寄存器中(因为它似乎与"x"约束相关),而是它留给了我.
如果有人可以为我编写约束(如果您愿意,请添加一个简短的解释),我真的很高兴.)
(注意:我正在编写这个项目仅供学习;有关冗余的评论是......呃,多余.;)
我正在尝试实现一个随机访问迭代器,但是我发现关于这个主题的文献很少,所以我将通过试验和错误结合维基多数运算符重载原型列表.到目前为止它运作良好,但我遇到了障碍.
代码如
exscape::string::iterator i = string_instance.begin();
std::cout << *i << std::endl;
Run Code Online (Sandbox Code Playgroud)
工作,并打印字符串的第一个字符.但是,*(i + 1)不起作用,*(1 + i)也不起作用.我的完整实现显然有点太多了,但这是它的要点:
namespace exscape {
class string {
friend class iterator;
...
public:
class iterator : public std::iterator<std::random_access_iterator_tag, char> {
...
char &operator*(void) {
return *p; // After some bounds checking
}
char *operator->(void) {
return p;
}
char &operator[](const int offset) {
return *(p + offset); // After some bounds checking
}
iterator &operator+=(const int offset) {
p += offset;
return *this;
}
const …Run Code Online (Sandbox Code Playgroud)