【发布时间】: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