[英]error: use of undeclared identifier VS Code
我只是想編譯一個簡單的程序,包括 .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;
}
}
你在編譯 .cpp 文件嗎? 您的編譯命令應該類似於g++ -Iinclude/ main.cpp src/LinkedList.cpp
。
另外,不要在 .hpp 中包含 .cpp 文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.