字符串流错误,而不是c_str

时间:2017-03-06 13:08:58

标签: 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尝试了同样的错误。 感谢您的时间, 珍妮

1 个答案:

答案 0 :(得分:2)

您可能有两个问题:第一个也是确定的问题是您将指向成员函数的指针传递给std::ofstream构造函数。你没有调用它。例如。

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

代替。

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

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