2017-04-24 101 views
-3

我一直对微软的Visual C++ 2010(32位系统)错误C2440:初始化:不能从 '的std :: string' 转换为 '双* []'

在编译阶段我得到一个错误,指出:

1>------ Build started: Project: pruebavecot, Configuration: Debug Win32 ------ 
1> pruebavecot.cpp 
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2057: expected constant expression 
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2466: cannot allocate an array of constant size 0 
1>c:\users\andresgraco\desktop\pruebavecot\pruebavecot\pruebavecot.cpp(64): error C2440: 'initializing' : cannot convert from 'std::string' to 'double *[]' 
1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

代码:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() { 
string line; 
ifstream myfile ("Vetor_Oscilacao.txt"); 
if (myfile.is_open()) 
{ 
int i=1; 
while (getline (myfile,line)) 
{ 
    cout << stod(line) << '\n'; 
    for(double i=1; i<100; i++) 
{ 
     double in[i]=line; 
} 
} 
myfile.close(); 
} 

else cout << "Unable to open file"; 
getchar(); 
return 0; 
} 

我试图从一个.txt文件接收数据,并将其存储在向量(在[1])在以后使用一个fftw。 .txt文件中的数据组织如下:

21.000000 
24.000000 
25.000000 
25.000000 
21.000000 
22.000000 
24.000000 
25.000000 
...(data #100) 

非常感谢您的帮助。

+1

'for(double i = 1; I <100;我为什么使用'double'而不是'int'或基于整数的循环计数器? – PaulMcKenzie

+1

你在哪里定义'in []'数组? – Alfabravo

+0

您需要使用'stod(line)'将行转换为double,以便将其分配给数组元素。 – Barmar

回答

0

您不能直接将字符串分配给数组。而你的for循环也没有意义。

尝试一些更喜欢这个:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

int main() 
{ 
    std::ifstream myfile ("Vetor_Oscilacao.txt"); 
    if (myfile.is_open()) 
    { 
     std::vector<double> in; 
     std::string line; 

     while (std::getline(myfile, line)) 
     { 
      double value = std::stod(line); 
      std::cout << value << '\n'; 
      in.push_back(value); 
     } 

     myfile.close(); 

     // use 'in' as needed... 
    } 
    else 
     std::cout << "Unable to open file"; 

    std::cin.get(); 
    return 0; 
} 

另外,因为所有的线是浮点数,你可以使用operator>>,而不是std::getline(),让它处理解析您:

#include <iostream> 
#include <fstream> 
#include <vector> 

int main() 
{ 
    std::ifstream myfile ("Vetor_Oscilacao.txt"); 
    if (myfile.is_open()) 
    { 
     std::vector<double> in; 
     double value; 

     while (myfile >> value) 
     { 
      std::cout << value << '\n'; 
      in.push_back(value); 
     } 

     myfile.close(); 

     // use 'in' as needed... 
    } 
    else 
     std::cout << "Unable to open file"; 

    std::cin.get(); 
    return 0; 
} 

然后可以使用std::copy()std::istream_iterator进行简化,而不是手动循环:

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <iterator> 
#include <algorithm> 

int main() 
{ 
    std::ifstream myfile ("Vetor_Oscilacao.txt"); 
    if (myfile.is_open()) 
    { 
     std::vector<double> in; 
     std::copy(
      std::istream_iterator<double>(myfile), 
      std::istream_iterator<double>(), 
      std::back_inserter(in) 
     ); 
     myfile.close(); 

     for (size_t i = 0; i < in.size(); ++i) 
      std::cout << in[i] << '\n'; 

     // use 'in' as needed... 
    } 
    else 
     std::cout << "Unable to open file"; 

    std::cin.get(); 
    return 0; 
} 
相关问题