代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."

Algebraic Tree Expression C++ error: Unresolved External Symbol "Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."

本文关键字:AlgebraicTreeExpression Public thiscall void 符号 C++ 表达式 错误 外部      更新时间:2023-10-16

我正在C++中处理一个家庭作业问题,其中我正在尝试围绕基本代数树节点类编写一个包装器代数树表达式类,用于基于输入字符串解析和计算代数方程。我得到的 3 个错误是:

Error   LNK2019 unresolved external symbol "public: __thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void)" (??1AlgebraicTreeExpression@@QAE@XZ) referenced in function _main   1   C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECMakeLists.txt    C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECalc6.cpp.obj
Error   LNK2019 unresolved external symbol "public: __thiscall AlgebraicTreeExpression::AlgebraicTreeExpression(char const *)" (??0AlgebraicTreeExpression@@QAE@PBD@Z) referenced in function _main 1   C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECMakeLists.txt    C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECalc6.cpp.obj
Error   LNK2019 unresolved external symbol "public: char * __thiscall AlgebraicTreeExpression::toString(void)const " (?toString@AlgebraicTreeExpression@@QBEPADXZ) referenced in function _main 1   C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECMakeLists.txt    C:Program Files (x86)Microsoft Visual Studio2017CommunityCommon7IDECalc6.cpp.obj

代数树表达式的相关部分.cpp是:

AlgebraicTreeExpression::AlgebraicTreeExpression():
  _root(new AlgebraicTreeNode()) {
}
AlgebraicTreeExpression::~AlgebraicTreeExpression() {
    if (_root != 0) delete _root;
}
char* AlgebraicTreeExpression::toString() const {
    return _root->AlgebraicTreeNode::toString();
}
// parser constructor
AlgebraicTreeExpression::AlgebraicTreeExpression(const char* expression):
  _root(new AlgebraicTreeNode(expression)) {
}

AlgebraicTreeNode.cpp的相关部分是:

AlgebraicTreeNode::~AlgebraicTreeNode() {
    delete this;
}
AlgebraicTreeNode::AlgebraicTreeNode() {
    //Default Constructor
    //this->id();
    //count();
    //_parent = (AlgebraicTreeNode*)0;
    _childLeft = 0; _childRight = 0;
    _type = AlgebraicTreeNodeType::INVALID;
    _value = 0;
}
AlgebraicTreeNode::AlgebraicTreeNode(AlgebraicTreeNode& src) {
    //Copy Constructor
    if (src._childLeft != (AlgebraicTreeNode*)(0)) {
        this->setChildLeft(src._childLeft);
        AlgebraicTreeNode(_childLeft);
    }
    if (src._childRight != (AlgebraicTreeNode*)(0)) {
        this->setChildRight(src._childRight);
        AlgebraicTreeNode(_childRight);
    }
    if (src._type == AlgebraicTreeNodeType::NUMBER) {
        _value = src._value;
        _type = src._type;
        _childLeft = 0; _childRight = 0;
    }
}
void AlgebraicTreeNode::setChildLeft(AlgebraicTreeNode* childLeft) {
    _childLeft = childLeft;
    _childLeft->_parent = this;
}
void AlgebraicTreeNode::setChildRight(AlgebraicTreeNode* childRight) {
    _childRight = childRight;
    _childRight->_parent = this;
}
char* AlgebraicTreeNode::toString() const {
    unsigned N = this->_toStringLength();
    char* str = new char[N];
    memset(str, '', N * sizeof(char));
    AlgebraicTreeNode::_toString(str);
    return str;
  return (char*)0;
}
AlgebraicTreeNode::AlgebraicTreeNode(const char* expression) {
    unsigned length = _parseExpression(expression);
}
// returns length of parsed substring 
unsigned AlgebraicTreeNode::_parseExpression(const char* expression) {
    static int integers[] = {0,1,2,3,4,5,6,7,8,9};
    std::vector<int> number(integers, integers + sizeof(integers) / sizeof(int));
    unsigned length = 0;
    int i = 0;
    std::vector<int>::size_type sz = number.size();

  if (*expression == '(') {
      length += 1;
      this->setChildLeft(new AlgebraicTreeNode());
      this->_value = 0;
      length += this->_childLeft->_parseExpression(expression + length);
      if (*(expression + length) == '+') {
          this->_type = AlgebraicTreeNodeType::ADD;
          length += 1;
      }
      else if (*(expression + length) == '-') {
          this->_type = AlgebraicTreeNodeType::SUBTRACT;
          length += 1;
      }
      else if (*(expression + length) == '*') {
          this->_type = AlgebraicTreeNodeType::MULTIPLY;
          length += 1;
      }
      else if (*(expression + length) == '/') {
          this->_type = AlgebraicTreeNodeType::DIVIDE;
          length += 1;
      }
      this->setChildRight(new AlgebraicTreeNode());
      length += this->_childRight->_parseExpression(expression + length);
      length += 1; //Placeholder for )
      return length;
  }
  while (i != sz) {
      printf("%i, %in", atoi(expression), number[i]);
      std::cout << typeid(atoi(expression)).name() << typeid(number[i]).name() << std::endl;
      if (atoi(expression + length) == number[i]) {
          this->_type = AlgebraicTreeNodeType::NUMBER;
          length += this->_parseNumber(expression + length);
          return length;
      }
      i++;
  }
  return length;
}
unsigned AlgebraicTreeNode::_parseNumber(const char* expression) {
  //unsigned length = 0;
  char *endptr;
  double value = strtod(expression,&endptr);
  this->_value = value;
  unsigned length = endptr - expression;
  return length;
}
unsigned AlgebraicTreeNode::_toStringLength() const {
   unsigned length = 1024;
   return length;
 }
 unsigned AlgebraicTreeNode::_toString(char* str) const {
   unsigned ntemp = 0;
   if (this->_type == INVALID) {
       return 0;
   }
   if (isOperation()) {
       ntemp += sprintf((str + ntemp), "(");
       if (this->_childLeft != (AlgebraicTreeNode*)0) {
           ntemp += this->_childLeft->_toString(str + ntemp);
       }
       if (_type == ADD) {
           ntemp += sprintf((str + ntemp), "+");
       }
       else if (_type == SUBTRACT) {
           ntemp += sprintf((str + ntemp), "-");
       }
       else if (_type == MULTIPLY) {
           ntemp += sprintf((str + ntemp), "*");
       }
       else if (_type == DIVIDE) {
           ntemp += sprintf((str + ntemp), "/");
       }
       if (this->_childRight != (AlgebraicTreeNode*)0) {
           ntemp += this->_childRight->_toString(str + ntemp);
       }
       ntemp += sprintf((str + ntemp), ")");
   }
   else if (isNumber()) {
       double thisvalue = this->_value;
       ntemp += sprintf((str + ntemp), "%.0f", thisvalue);
   }
   return ntemp;
 }

这可能是一个小修复,但感谢您的任何帮助!

您是否与 AlgebraicTreeExpression.cpp 对象文件链接在一起?(即对于 MSVC,它必须位于同一项目中,或者如果它是一个库,则需要指定要链接它)。

除此之外,请参阅此处:对象文件中未解析的外部符号

顺便说一句。

AlgebraicTreeNode::~AlgebraicTreeNode() {
    delete this;
}

请不要这样做,而是在对象上调用delete,这将调用析构函数并释放内存(而不是像这里那样从析构函数内部调用delete)。

(如果在堆栈上创建对象,这会特别狠狠地咬你)

相关文章:
  • 没有找到相关文章