什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?
随意编辑/添加您自己的.
c++ c++-faq linker-errors unresolved-external undefined-reference
我有一些模板代码,我宁愿存储在CPP文件中而不是标题中的内联.我知道只要您知道将使用哪些模板类型,就可以完成此操作.例如:
.h文件
class foo
{
public:
template <typename T>
void do(const T& t);
};
Run Code Online (Sandbox Code Playgroud)
.cpp文件
template <typename T>
void foo::do(const T& t)
{
// Do something with t
}
template void foo::do<int>(const int&);
template void foo::do<std::string>(const std::string&);
Run Code Online (Sandbox Code Playgroud)
注意最后两行--foo :: do模板函数仅用于int和std :: strings,因此这些定义意味着应用程序将链接.
我的问题是 - 这是一个讨厌的黑客还是会与其他编译器/链接器一起使用?我目前只在VS2008上使用此代码,但是想要移植到其他环境.
这个问题已在C#/ .Net的背景下提出.
现在我想学习C++中结构和类之间的区别.请讨论技术差异以及在OO设计中选择其中一个的原因.
我将从一个明显的区别开始:
public:or private:,则结构的成员默认是公共的; 默认情况下,类的成员是私有的.我确信在C++规范的模糊角落中还有其他差异.
我正在尝试使用自定义类作为关键字unordered_map,如下所示:
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
class node;
class Solution;
class Node {
public:
int a;
int b;
int c;
Node(){}
Node(vector<int> v) {
sort(v.begin(), v.end());
a = v[0];
b = v[1];
c = v[2];
}
bool operator==(Node i) {
if ( i.a==this->a && i.b==this->b &&i.c==this->c ) {
return true;
} else {
return false;
}
}
};
int main() {
unordered_map<Node, int> m;
vector<int> v;
v.push_back(3);
v.push_back(8);
v.push_back(9);
Node n(v);
m[n] = 0; …Run Code Online (Sandbox Code Playgroud) 我不知道为什么会发生这种情况,因为我认为我已经正确地声明和定义了所有内容.
我有以下程序,使用模板设计.这是一个简单的队列实现,其成员函数为"add","substract"和"print".
我已经在精细的"nodo_colaypila.h"中为队列定义了节点:
#ifndef NODO_COLAYPILA_H
#define NODO_COLAYPILA_H
#include <iostream>
template <class T> class cola;
template <class T> class nodo_colaypila
{
T elem;
nodo_colaypila<T>* sig;
friend class cola<T>;
public:
nodo_colaypila(T, nodo_colaypila<T>*);
};
Run Code Online (Sandbox Code Playgroud)
然后在"nodo_colaypila.cpp"中实现
#include "nodo_colaypila.h"
#include <iostream>
template <class T> nodo_colaypila<T>::nodo_colaypila(T a, nodo_colaypila<T>* siguiente = NULL)
{
elem = a;
sig = siguiente;//ctor
}
Run Code Online (Sandbox Code Playgroud)
然后,队列模板类的定义和声明及其功能:
"cola.h":
#ifndef COLA_H
#define COLA_H
#include "nodo_colaypila.h"
template <class T> class cola
{
nodo_colaypila<T>* ult, pri;
public:
cola<T>();
void anade(T&);
T saca();
void print() const;
virtual …Run Code Online (Sandbox Code Playgroud) 我有一个带有一个参数的模板函数.我必须实例化该函数而不调用该函数意味着我必须实例化.
我有这个功能:
template <class T> int function_name(T a) {}
Run Code Online (Sandbox Code Playgroud)
我实例化了这样的函数:
template int function_name<int>(int);
Run Code Online (Sandbox Code Playgroud)
但是我遇到了以下错误:
error: expected primary-expression before 'template'
error: expected `;' before 'template'
Run Code Online (Sandbox Code Playgroud) 由于模板是在头文件中定义的,编译器能够确定内联函数是否有利,它是否有意义?我听说现代编译器更好地知道何时内联函数并忽略inline提示.
编辑:我想接受这两个答案,但这是不可能的.为了解决这个问题,我接受了菲涅耳的回答,因为它收到了大多数选票并且他是正式的,但正如我在评论中所提到的,我从不同的角度考虑Puppy和组件10的答案也是正确的. .
问题出在C++语义中,在inline关键字和内联的情况下并不严格.Phresnel说"如果你的意思是写内联",但实际意义inline并不明确,因为它从原来的意义演变为"阻止编译人员讨论ODR违规"的指令,正如Puppy所说.
当我使用模板为类编写C++代码并在源(CPP)文件和标题(H)文件之间拆分代码时,在链接最终可执行文件时会出现大量"未解析的外部符号"错误,尽管目标文件正确构建并包含在链接中.这里发生了什么,我该如何解决?
我在尝试编译一个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) 是否可以在"现代C++"(C++ 17或更高版本)中将字符串文字作为参数传递给C++模板?
我意识到你可以用构造函数参数做到这一点; 我只是认为将它作为模板参数更方便,而不是深埋在cpp文件中.如果这可能是现代C++的一个新功能,我很好奇.请参阅下面的伪代码我正在尝试做的事情:
伪代码示例:
// Header File /////////////////////////
template<constexpr string Name>
class ModuleBase {
public:
ModuleBase();
string name;
};
class xyz : ModuleBase<"xyz"> {
public:
xyz();
};
// Cpp File //////////////////////////
template<string_literal Name>
ModuleBase<Name>::ModuleBase() {
name = Name;
}
xyz::xyz() : ModuleBase() {
}
Run Code Online (Sandbox Code Playgroud)