C++ 未知符号 - 难以发现 .h 和 .cpp 文件之间的差异
C++ Unknown Symbol - Difficulty Finding Discrepancy Between .h and .cpp File
在我的 C++ 程序中,出现了这个错误:
Undefined symbols for architecture x86_64:
"MathExpression::layerFunctions", referenced from:
MathExpression::initializeLayerFunctions() in MathExpression.cpp.o
MathExpression::layer(MathExpression&, MathExpression::Operation,
std::__1::vector<short, std::__1::allocator<short> >&) in
MathExpression.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[3]: *** [MathTestGenerator] Error 1
make[2]: *** [CMakeFiles/MathTestGenerator.dir/all] Error 2
make[1]: *** [CMakeFiles/MathTestGenerator.dir/rule] Error 2
make: *** [MathTestGenerator] Error 2
我的头文件中有这个声明:
static std::vector<std::function<void(MathExpression &, std::vector<NumberType> &)>> layerFunctions;
在我的 .cpp 文件中,我有:
std::vector<std::function<void(MathExpression &, std::vector<MathExpression::NumberType> &)>> layerFunctions(static_cast<MathExpression::OperationType> (MathExpression::Operation::EOE));
连同此功能:
void MathExpression::initializeLayerFunctions() {
layerFunctions.resize(static_cast<OperationType>(Operation::EOE));
layerFunctions[static_cast<unsigned long>(Operation::addition)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::addition), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::subtraction)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::subtraction), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::EOE)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
// Throw or assert or something.
};
}
我不确定自己做错了什么。据我所知,我的声明是匹配的,但我无法摆脱错误。有人会对如何解决这个问题有见识吗?未来感谢。
作为参考,这里分别是我的头文件和 .cpp 文件(请记住,这是我的第一个 C++ 程序,所以我确定缺少约定):
#ifndef MATHTESTGENERATOR_MATHEXPRESSION_H
#define MATHTESTGENERATOR_MATHEXPRESSION_H
#include <vector>
#include <functional>
class MathExpression {
public:
using FieldType = unsigned char;
using OperationType = unsigned char;
using NumberType = short int;
using CharType = char;
enum class Field : FieldType {
integers,
EOE // rational, real, complex.
};
enum class Operation : OperationType {
addition,
subtraction,
EOE // multiplication, division, absolute value, radical
};
explicit MathExpression(std::vector<CharType>);
std::vector<CharType> string;
static void print(MathExpression &);
static void layer(MathExpression &, Operation,
std::vector<NumberType> &);
static void initialize();
private:
static char operationToChar(OperationType);
static char operationToChar(Operation);
static std::vector<std::function<void(MathExpression &,
std::vector<NumberType> &)>> layerFunctions;
static void initializeLayerFunctions();
};
#endif //MATHTESTGENERATOR_MATHEXPRESSION_H
和
#include "MathExpression.h"
#include <list>
#include <iostream>
std::vector<std::function<void(MathExpression &,
std::vector<MathExpression::NumberType> &)>>
layerFunctions(static_cast<MathExpression::OperationType>, (MathExpression::Operation::EOE));
void MathExpression::initialize() {
initializeLayerFunctions();
}
void MathExpression::initializeLayerFunctions() {
layerFunctions.resize(static_cast<OperationType>(Operation::EOE));
layerFunctions[static_cast<unsigned long>(Operation::addition)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::addition), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::subtraction)] = []
(MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::subtraction), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::EOE)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
// Throw or assert or something.
};
}
char MathExpression::operationToChar(OperationType ordinal) {
return operationToChar(static_cast<Operation>(ordinal));
}
char MathExpression::operationToChar(Operation op) {
switch(op) {
case Operation::addition : return '+';
case Operation::subtraction : return '-';
default : return '_';
}
}
MathExpression::MathExpression(std::vector<CharType> exp) {
this->string = std::vector<CharType>(exp);
}
void MathExpression::print(MathExpression &exp) {
for(int i = 0; i < exp.string.size(); i++) {
std::cout << exp.string[i];
}
}
void MathExpression::layer(MathExpression &exp,
MathExpression::Operation op, std::vector<NumberType> &otherArgs) {
layerFunctions[static_cast<OperationType>(op)](exp, otherArgs);
}
问题出在 MathExpression.CPP 内。
您定义了 layerFunctions,但由于它不在 MathExpressions 命名空间内,I.E. MathExpressions::layerFunctions
,它说你的 header 里面有一个链接器错误。
通过将该名称空间添加到您的函数声明中,您的问题应该不再是问题。
如果您快速 google.
,您可以到处找到有关链接器错误的信息
在我的 C++ 程序中,出现了这个错误:
Undefined symbols for architecture x86_64:
"MathExpression::layerFunctions", referenced from:
MathExpression::initializeLayerFunctions() in MathExpression.cpp.o
MathExpression::layer(MathExpression&, MathExpression::Operation,
std::__1::vector<short, std::__1::allocator<short> >&) in
MathExpression.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[3]: *** [MathTestGenerator] Error 1
make[2]: *** [CMakeFiles/MathTestGenerator.dir/all] Error 2
make[1]: *** [CMakeFiles/MathTestGenerator.dir/rule] Error 2
make: *** [MathTestGenerator] Error 2
我的头文件中有这个声明:
static std::vector<std::function<void(MathExpression &, std::vector<NumberType> &)>> layerFunctions;
在我的 .cpp 文件中,我有:
std::vector<std::function<void(MathExpression &, std::vector<MathExpression::NumberType> &)>> layerFunctions(static_cast<MathExpression::OperationType> (MathExpression::Operation::EOE));
连同此功能:
void MathExpression::initializeLayerFunctions() {
layerFunctions.resize(static_cast<OperationType>(Operation::EOE));
layerFunctions[static_cast<unsigned long>(Operation::addition)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::addition), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::subtraction)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::subtraction), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::EOE)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
// Throw or assert or something.
};
}
我不确定自己做错了什么。据我所知,我的声明是匹配的,但我无法摆脱错误。有人会对如何解决这个问题有见识吗?未来感谢。
作为参考,这里分别是我的头文件和 .cpp 文件(请记住,这是我的第一个 C++ 程序,所以我确定缺少约定):
#ifndef MATHTESTGENERATOR_MATHEXPRESSION_H
#define MATHTESTGENERATOR_MATHEXPRESSION_H
#include <vector>
#include <functional>
class MathExpression {
public:
using FieldType = unsigned char;
using OperationType = unsigned char;
using NumberType = short int;
using CharType = char;
enum class Field : FieldType {
integers,
EOE // rational, real, complex.
};
enum class Operation : OperationType {
addition,
subtraction,
EOE // multiplication, division, absolute value, radical
};
explicit MathExpression(std::vector<CharType>);
std::vector<CharType> string;
static void print(MathExpression &);
static void layer(MathExpression &, Operation,
std::vector<NumberType> &);
static void initialize();
private:
static char operationToChar(OperationType);
static char operationToChar(Operation);
static std::vector<std::function<void(MathExpression &,
std::vector<NumberType> &)>> layerFunctions;
static void initializeLayerFunctions();
};
#endif //MATHTESTGENERATOR_MATHEXPRESSION_H
和
#include "MathExpression.h"
#include <list>
#include <iostream>
std::vector<std::function<void(MathExpression &,
std::vector<MathExpression::NumberType> &)>>
layerFunctions(static_cast<MathExpression::OperationType>, (MathExpression::Operation::EOE));
void MathExpression::initialize() {
initializeLayerFunctions();
}
void MathExpression::initializeLayerFunctions() {
layerFunctions.resize(static_cast<OperationType>(Operation::EOE));
layerFunctions[static_cast<unsigned long>(Operation::addition)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::addition), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::subtraction)] = []
(MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
exp.string.insert(exp.string.end(), {' ', operationToChar(Operation::subtraction), ' ', static_cast<CharType>
(otherArgs[0])});
};
layerFunctions[static_cast<unsigned long>(Operation::EOE)] = [] (MathExpression & exp, std::vector<NumberType> & otherArgs) -> void {
// Throw or assert or something.
};
}
char MathExpression::operationToChar(OperationType ordinal) {
return operationToChar(static_cast<Operation>(ordinal));
}
char MathExpression::operationToChar(Operation op) {
switch(op) {
case Operation::addition : return '+';
case Operation::subtraction : return '-';
default : return '_';
}
}
MathExpression::MathExpression(std::vector<CharType> exp) {
this->string = std::vector<CharType>(exp);
}
void MathExpression::print(MathExpression &exp) {
for(int i = 0; i < exp.string.size(); i++) {
std::cout << exp.string[i];
}
}
void MathExpression::layer(MathExpression &exp,
MathExpression::Operation op, std::vector<NumberType> &otherArgs) {
layerFunctions[static_cast<OperationType>(op)](exp, otherArgs);
}
问题出在 MathExpression.CPP 内。
您定义了 layerFunctions,但由于它不在 MathExpressions 命名空间内,I.E. MathExpressions::layerFunctions
,它说你的 header 里面有一个链接器错误。
通过将该名称空间添加到您的函数声明中,您的问题应该不再是问题。
如果您快速 google.