c++ - 字符串流错误,不是 c_str

标签 c++ linux

我有一段代码可以在 Visual Studio 中运行,但无法在我的大学集群上编译。集群使用icc编译器,我的makefile在这里:

CC=icc
DEPENDENCY = Polymer.h

%.o:%.c $(DEPENDENCY)
        $(CC) -03 -xSSE4.2 -axAVX, CORE-AVX-I, CORE-AVX2
PolymerExe: Main.o Polymer.o
        CC -o PoymerExe Main.o Polymer.o -I

这些错误似乎都与字符串有关,因此我将仅粘贴那些相关的位(否则这是大量代码)。

在主程序之前

#include"Polymer.h"
#include<iostream>
#include<fstream>
#include<ctime>
#include<iomanip>
#include <sstream>
#include <stdlib.h>

using namespace std;

内部主

stringstream myName;
myName << "DeltaGdependence_given" << "DeltaG_Pol" << DeltaG_Pol << ".txt";
ofstream aFile(myName.str);
aFile << deltaG << "\t" << Velocity << "\t" << Error_Given_R << "\t" << Error_Given_W << "\t" << Error << endl;
aFile.close();

我收到与此非常相似的错误 Using sstream in to provide the name of output file in ofstream但如果我将其更改为 myName.c_str 则不起作用

myName.str 的完整错误:

Main.cpp:27:30: error: no matching function for call to ‘std::basic_ofstream<char>::basic_ofstream(std::basic_stringstream<char>::__string_type)’
   ofstream aFile(myName.str());
                              ^
Main.cpp:27:30: note: candidates are:
In file included from Main.cpp:4:0:
/usr/include/c++/4.8.2/fstream:640:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       basic_ofstream(const char* __s,
       ^
/usr/include/c++/4.8.2/fstream:640:7: note:   no known conversion for argument 1 from ‘std::basic_stringstream<char>::__string_type {aka std::basic_string<char>}’ to ‘const char*’
/usr/include/c++/4.8.2/fstream:625:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits<char>]
       basic_ofstream(): __ostream_type(), _M_filebuf()
       ^
/usr/include/c++/4.8.2/fstream:625:7: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8.2/fstream:599:11: note: std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)
     class basic_ofstream : public basic_ostream<_CharT,_Traits>
           ^
/usr/include/c++/4.8.2/fstream:599:11: note:   no known conversion for argument 1 from ‘std::basic_stringstream<char>::__string_type {aka std::basic_string<char>}’ to ‘const std::basic_ofstream<char>&’
make: *** [Main.o] Error 1

如果我将 myName.str 更改为 myName.c_str,我会在 Visual Studio 和集群中收到此错误。

Main.cpp: In function ‘int main()’:
Main.cpp:27:25: error: ‘std::stringstream’ has no member named ‘c_str’
   ofstream aFile(myName.c_str);
                         ^
make: *** [Main.o] Error 1

我也尝试过仅使用 myName,但出现相同的错误。 感谢您的时间, 珍妮

最佳答案

您可能会遇到两个问题:第一个也是最终的问题是您将指向成员函数的指针传递给 std::ofstream 构造函数。您不调用它。做例如

ofstream aFile(myName.str());
//                       ^^
// Note parentheses to call the member function

相反。

另一个可能的问题是,C++11 标准提供了使用 std::string 作为文件流 open 调用(以及在文件流构造函数中)的参数的可能性。在此之前,您只能使用 const char*

因此,如果英特尔 C++ 编译器支持 C++11(或更高版本),那么您可能需要在编译时添加一个标志以启用 C++11(或更高版本)。或者,当您打开文件流时,您必须传递 const char*

关于c++ - 字符串流错误,不是 c_str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42626487/

相关文章:

mysql - 在 Linux Web 服务器上公开 Access 数据库

c++ - C++ 标准的哪一部分允许在括号中声明变量?

c++ - 使用 Boost.Phoenix 有什么好处?

c# - 通过 .NetCore SQLConnection 到 MariaDB localhost

ruby - 如何输出转义字符?

linux - perl one liner + 如何在 perl line liner 上给出标准输出

c++ - 为什么我们在这里增加 ostream 迭代器

c++ - 类模板和模板类

java - 在 Java 中修复你的时间步长

python - 获取 pyswip 包的版本号