2016-11-15 78 views
-2

一個奇怪的事情時,我嘗試在輸出文件寫入沒有發生寫作:輸出文件C++

int main(int argc, char* argv[]) 
{ 


    std::string outputFile = "fitness.out"; 
    ofstream output(outputFile.c_str()); 


    FitnessFactory fitnessFactory; 

    //if i write on file here it's ok 

    vector<string> params; 
    params[0]= 10; 
    params[1]=120; 

    //if i write on file here not ok, file empty 

    Strategy* strategy = fitnessFactory.getStrategy("OneMax", params); 
    vector<string> individual; 


for(unsigned i = 0; i < argc-1; i++){ 

string inputFile = argv[i+1]; 
ifstream input(inputFile.c_str()); 
while(input.good()) 
{ 
    string temp; 
    input>>temp; 
    individual.push_back(temp); 
} 
input.close(); 
// if i write on file, before call strategy->evaluate,for example 
// output << "hello"<<endl; 
// file contains "hello" 
string res = strategy->evaluate(individual); 
//if i write here file is empty 
//output<< res << endl; 
} 

其他CPP文件分別是:

factoryMethod.cpp

 #include <stdio.h> 
#include "strategy.cpp" 
using namespace std; 
class factoryMethod 
{ 

public: 
    virtual Strategy* getStrategy(string x, vector<string> params) = 0; 


}; 

Strategy* factoryMethod :: getStrategy(string x, vector<string> 
params) 
{ 
} 


class FitnessFactory : public factoryMethod 
{ 
public: 
     Strategy* getStrategy(string x, vector<string> params) { 

     Strategy* s; 

      if(x=="OneMax") 
      s = new OneMax(); 
      else if (x=="Weierstrass") s = new Weiestrass(); 
      return s; 
     } 
}; 

策略.cpp

std::string Convert (float number){ 
std::ostringstream buff; 
buff<<number; 
return buff.str(); 
} 

class Strategy 
{ 
public: 
    virtual string evaluate(vector<string> v) = 0; 
    virtual void strategy(vector<string> params); 
public: vector<string> fparams; 
}; 

string Strategy :: evaluate(vector<string> v) 
{ 

} 

void Strategy :: strategy(vector<string> v){ 
fparams = v; 
} 

class OneMax : public Strategy 
{ 
public: 
     string evaluate(vector<string> v) { 
      string x = v[0]; 
     int count = 0; 
for (int i = 0; i < x.size(); i++) 
     if (x[i] == '1') count = count + 1; 

      string ret = static_cast<ostringstream*>(& 
(ostringstream() << count))->str(); 
      return ret; 
     } 
}; 
class Weiestrass: public Strategy 
{ 
public: 
    string evaluate(vector<string> v) { 
    // int repetitions = atoi(fparams[0].c_str()); 
// int iterations = atoi(fparams[1].c_str()); 
int repetitions = 10; 
int iterations = 120; 
vector<float> x; 
for(int i = 0; i < v.size(); i++){ 
    float temp = ::atof(v[i].c_str()); 
    x[i] = temp; 
} 
float fRes=0, fSum, b=2, h=0.5; 

for (int j = 0; j < repetitions; j++) 
{ 

     fRes = 0; 
     for (int i = 0; i < x.size(); i++) 
    { 
      fSum=0; 
      for (int k = 0; k < iterations; k++) fSum += pow(b,-  
(float)k*h) * sin(pow(b,(float)k)*x[i]); 
      fRes += abs(fSum); 
     } 
} 

string s = Convert(fRes); 
return s; 
} 
}; 

因此w如果我試圖在調用之後編寫函數的結果或任何其他值來評估文件是空白的。 我不明白爲什麼?? !!

+0

'的std :: VECTOR'沒有元素開始,所以'PARAMS [0] = ...'不正確。相反,'params.push_back(...)'。 – crashmstr

+0

另外,學習如何在調試器中逐步執行程序。這應該可以幫助您找到並解決問題。 – crashmstr

+0

爲什麼在其他.cpp文件中包含.cpp文件?閱讀關於頭文件。爲什麼使用C頭文件像'stdio.h'? –

回答

1
vector<string> params; 
params[0]= 10; 
params[1]=120; 

編號std::vector::operator[]()不會增加您內部的元素數量。要麼vector<string> params(2);上創建結構,params.resize(2) 2個元素來創建分配前2個元素,或

params.push_back("10"); 
params.push_back("120");