简体   繁体   中英

error: use of undeclared identifier VS Code

I am simply trying to compile a simple program including a .cpp and .h(.hpp for VS code Purposes) Whenever I Put the implementation for the Constructor in the .hpp file and not include the .cpp, the code will compile. However, whenever i separate the Implementation into the .cpp file i get

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

I have tried including headguards but use #pragma once, i still get the same error. Here is my main.cpp

#include "LinkList.hpp"


int main()
{


  LinkedChar("h");


  return 0;
}

Here is my .hpp File

#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;
};

my cpp file

#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;

  }

}

Are you compiling the .cpp file? Your compilation command should look like g++ -Iinclude/ main.cpp src/LinkedList.cpp .

Also, don't include the .cpp file in your .hpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM