'struct std::string'没有名为'c_string'的成员
'struct std::string' has no member named 'c_string'
我收到错误消息'struct std::string'在我的代码中没有名为'c_string'的成员。 我基于我的教科书中的代码构建此代码,该代码使用我的编译器成功编写了代码。 哪些语法错误可能导致此特定错误? (在第11行(
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string payroll = "table.dat";
ofstream MyCout;
MyCout.open(payroll.c_string());
if (MyCout.fail())
{
cout<<"Your file was not found";
exit(1);
}
MyCout<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint)
<<setprecision(2);
MyCout << "B Caldwell 555-88-2222"<<17.32<<37<<endl
<< "Next Line"<<0.00<<00<<endl;
<< "Next Line"<<0.00<<00<<endl;
MyCout.close();
cout<<"The file "<<payroll<<" has been successfully written"<<endl;
system ("Pause");
return 0;
}
我不确定这是否有帮助,但这是我基于它的代码
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string filename = "prices.dat";
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "The file was not successfully opened" << endl;
exit(1);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
outFile << "Mats " << 39.95 << endl
<< "Bulbs " << 3.22 << endl
<< "Fuses " << 1.08 << endl;
outFile.close();
cout << "The file " << filename << " has been successfully written."<<endl;
return 0;
}
std::string
没有称为c_string
的函数或成员,因此确切的编译器错误。您可以在线找到有关哪些成员和函数可用于调用标准库对象的大量信息,例如此处。
如果您仔细查看所引用的代码,您会发现您打算改用c_str()
。
他们的代码:
ofstream outFile;
outFile.open(filename.c_str());
您的代码:
ofstream MyCout;
MyCout.open(payroll.c_string());
解决方案:
ofstream MyCout;
MyCout.open(payroll.c_str());
相关文章:
- 反转没有循环的字符串,C++中只有 std::string 成员
- C++ 入门 12.1:我可以在类中使用静态向量成员实现 StrBlob<string> 吗?
- 使用<string>提升::序列化序列化具有 vector<unique_ptr> 成员的类
- 如何使用<string>初始值设定项列表初始化unordered_map<字符串、unordered_set>成员?
- C++ 错误:'std::string'没有成员
- 我应该如何处理:错误 C2039 "分配":不是 Visual Studio 2017 中"cl::string"的成员?
- 'struct std::string'没有名为'c_string'的成员
- 在结构的联合成员中使用std::string时,无法引用默认构造函数
- 如何将STD :: String的值复制到Char数组类型的结构成员
- 如何将 std::vector<std::string> 转换为 boost::spirit 中结构的成员?
- std::string::substr成员函数的复杂性是什么?
- 不要使用 "const char *" 作为类的成员,如果你真的是"std::string"
- 获取指向成员std::string::size的指针无法与libc++链接,但可以与libstdc++一起使用
- 工会不会接受第 'string' 类成员
- std::string 作为成员函数的参数的奇怪行为
- 使用 move std::string 的 .data() 成员不适用于小字符串?
- 如何创建静态常量成员 std::string 数组
- 当传入具有std::string成员的结构时,C++-gdb会给出意外的输出
- 结构体的std::string成员的EXC_BAD_ACCESS错误
- 为什么push_back会在上行对象的string成员中抛出一个char ?