在我的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);
}
答案 0 :(得分:1)
问题出在MathExpression.CPP中。
您定义了layerFunctions,但由于它不在MathExpressions命名空间内,因此I.E. MathExpressions::layerFunctions
,它说你的标题中有一个链接器错误。
通过在函数声明中添加该命名空间,您的问题应该不再是一个问题。
如果你给他们一个快速谷歌,你可以找到有关链接器错误的信息。