编译错误C++
Compile error C++
我是C++新手。当我编译此代码时,编译器报告错误-
主.cpp-
#include <iostream>
#include <string>
#include "main.h"
using namespace std;
string strings::getstr(string str)
{
return str;
}
int main()
{
strings strstr;
string constr;
string msg;
msg = "Hello World!";
constr = strstr.getstr(msg);
cout << constr;
return 0;
}
主.h-
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
string getstr (string str);
};
#endif // MAIN_H_INCLUDED
错误-
error: 'string' does not name a type
error: no 'std::string strings::getstr(std::string)' member function declared in class 'strings'
error: In function 'int main()':
error: 'class strings' has no member named 'getstr'
我正在使用代码::块和 gcc我写了这个简单的代码,因为我正在处理一个项目,当我想编译时,我总是得到
"字符串"不命名类型
对不起,英语不好...
字符串类的正确名称是"std::string",因为它是在"std"命名空间中声明的("cout"也是如此)。将"string"更改为"std::string"并使用"std::cout"而不是"cout"后,您的程序将正确编译。
另一种方法是将"std"作为第一个命名空间:
#include <string>
using namespace std;
就个人而言,我不喜欢使用"使用命名空间..."。(我很难跟踪不同的命名空间)。
这可以解决问题
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
std::string getstr(std::string str);
};
#endif // MAIN_H_INCLUDED
在
标题后使用 using namespace std;
main.h
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'