【问题标题】:error: use of undeclared identifier VS Code错误:使用未声明的标识符 VS 代码
【发布时间】:2020-12-19 06:26:12
【问题描述】:

我只是想编译一个简单的程序,包括 .cpp 和 .h(用于 VS 代码目的的.hpp) 每当我将构造函数的实现放在 .hpp 文件中并且不包含 .cpp 时,代码都会编译。但是,每当我将实现分成 .cpp 文件时,我都会得到

error: use of undeclared identifier 'std' LinkedChar::(std::string s)

我尝试过包含头罩,但使用 #pragma 一次,我仍然得到同样的错误。 这是我的 main.cpp

#include "LinkList.hpp"


int main()
{


  LinkedChar("h");


  return 0;
}

这是我的 .hpp 文件

#pragma once
#include "LinkList.cpp"
#include <iostream>
#include <string>

//Create a LinkList
class Node
{
  public:


  Node *next;
  char data; //The data will hold a character 
};

class LinkedChar
{   
  public:
  //LinkedChar(): head(NULL), tail(NULL){}
  LinkedChar(std::string s);


  private:
  Node* tail;
  Node* head;
};

我的 cpp 文件

#include "LinkList.hpp"

LinkedChar::LinkedChar(std::string s)
{

  int length = s.length();
  Node* temp = new Node;
  head = nullptr;



  if(length==1)//If there is only one letter
  {
     temp->data=s[0];
     head = temp;     
     tail = temp;
     temp->next = nullptr;
     std::cout<<temp->data;

  }

  else
  {
     for(int i = 1; i<length; i++)

     temp->data = s[i];
     temp->next = nullptr;
     tail = temp;

  }

}

【问题讨论】:

  • 1) 不要#include .cpp 文件。 2) 现在,.hpp 和 .cpp 之间有一个相互的#include
  • 谢谢,但是当我现在尝试运行时,我得到一个 Permission denied 错误
  • 如果LinkedChar::(std::string s) 是错误消息中的真实代码,那么您将显示不相关的代码。
  • 使用 Make 或 CMake 或 MSBuild 等构建工具

标签: c++ visual-studio-code undeclared-identifier


【解决方案1】:

您正在编译 .cpp 文件吗?您的编译命令应类似于 g++ -Iinclude/ main.cpp src/LinkedList.cpp

另外,不要在 .hpp 中包含 .cpp 文件

【讨论】:

  • 谢谢,我取出了包含,但是我仍然无法编译。无论如何我可以在 C++11 的 VS 代码中使用运行按钮,还是需要通过终端?似乎可能未配置 json 文件中的某些内容,但我不确定如何配置。
  • VS 代码总是使用终端进行编译。你绝对可以自动化这个过程。查看this useful page about configuring the debugger。这并不难。默认值通常非常好,只需要一些调整。如果你不能让它工作,也许你可以提供你的 tasks.json 文件。
  • 架构 x86_64 的未定义符号:“LinkedChar::LinkedChar(std::__1::basic_string, std::__1::allocator >)”,引用自:mainfunc-403067.o ld 中的 _main:未找到架构 x86_64 的符号
猜你喜欢
  • 1970-01-01
  • 2020-05-11
  • 2013-03-12
  • 1970-01-01
  • 2014-08-04
  • 2020-05-05
  • 2019-09-29
  • 1970-01-01
相关资源
最近更新 更多