2014-05-03 178 views
-1

我一直有兩天這個相同的錯誤:直到現在我的代碼的最佳版本是在下面,編譯器不斷抱怨說「沒有操作符< <匹配這些操作數「,雖然我做了#include,正如其他主題中所建議的那樣。 另外我不知道我是否應該把所有那裏的標題和東西放在我的帖子中,因爲它比較擁擠這種方式..沒有操作符<<與這些操作數匹配WITH #include <string>

整個程序並非真正的所有相關,我試圖創建一個純虛函數「vector Toestand(int)」(所以它應該返回一個向量並且有一個int作爲參數)。由於某種原因,這從來不起作用,這就是爲什麼我使用了其他程序,我確信它確實起作用,我完全剝離了它。不過,至今沒有運氣..

我標誌着表明在什麼點C的最重要的部分++感到不舒服

基類的頭:

#ifndef BasisToestand_H 
#define BasisToestand_H 

    #include<vector> 
    using std::vector; 
    #include <string> 
    using std::string; 


class BasisToestand 
{ 

public: 

    BasisToestand (const string="geen naam"); 
    virtual ~BasisToestand();   

    //void setName(const string); 

    // Get Functions are all declared const 
    const string getName() const; 
    virtual const double getVal(double) const = 0;    //THIS WORKS 
    //virtual const vector<double> Toestand(int) const = 0; //THIS DOES NOT 

    // Overloaded operators is also declared const and virtual 
    virtual const double operator()(double = 0.) const = 0; //THIS WORKS 



private: 

    string T_Naam; 

}; 

#endif 

基類CPP:

#include <iostream> 
using std::cout; 
using std::endl; 

#include<vector> 
using std::vector; 
#include <string> 
using std::string; 
#include <cstdlib> 
using std::exit; 

#include "BasisToestand.h" 

BasisToestand::BasisToestand (const string nieuwe_naam) 
    : T_Naam(nieuwe_naam) 
{ 
    cout << "calling base class BasisToestand constructor for " << T_Naam << endl; 
} 

BasisToestand::~BasisToestand() 
{ 
    cout << "calling base class BasisToestand destructor for " << T_Naam << endl; 
} 

const string BasisToestand::getName() const 
{ 
    return T_Naam; 
} 

派生類標題:

#ifndef T_Tsunami_H // preprocessor wrapper 
#define T_Tsunami_H 

#include <string> 
using std::string; 

#include "BasisToestand.h" // base class header 

const static double PI = 3.1415926535897932384626433832795; 

class T_Tsunami : public BasisToestand 
{ 

public: 

    // Constructor with default arguments 
    T_Tsunami (const double norm = 1., const double mean = 0., 
      const double sigma = 1., const string="T_Tsunami"); 

    ~T_Tsunami(); // destructor 

    // Set Functions 
    void setNorm(const double norm = 1.); 
    void setMean(const double mean = 0.); 
    void setSigma(const double sigma = 1.); 

    // Get Functions are all declared const 
    const double getNorm() const; 
    const double getMean() const; 
    const double getSigma() const; 

    virtual const double getVal(double) const;   //THIS WORKS 
//virtual const vector<double> Toestand(int) const; //PROBLEM 

// Overloaded operators is also declared const 
virtual const double operator()(double = 0.) const; //THIS WORKS 

private: 

    double p0; 
    double p1; 
    double p2; 

}; 

派生類的.cpp

#include <iostream> 
using std::cout; 
using std::endl; 

#include <cmath> 

#include "T_Tsunami.h" // Only T_Tsunami header file needed 

T_Tsunami::T_Tsunami (const double norm, const double mean, 
      const double sigma, const string nieuwe_naam) 
     : BasisToestand(nieuwe_naam), 
     p0(norm), 
     p1(mean), 
     p2(sigma) 
{ 
    cout << "calling derived class T_Tsunami constructor for " << getName() << endl; 
} 

T_Tsunami::~T_Tsunami() 
{ 
    cout << "calling derived class T_Tsunami destructor for " << getName() << endl; 
} 

const double T_Tsunami::getVal(double x) const 
{ 
    return p0/p2/(sqrt(2*PI))*exp(-pow((x-p1),2)/(2*pow(p2,2))); 
} 

const double T_Tsunami::operator()(double x) const // overloaded() operator WORKS 
{ 
    return getVal(x); 
} 

void T_Tsunami::setNorm (const double norm) 
{ 
    p0 = norm; 
} 
void T_Tsunami::setMean (const double mean) 
{ 
    p1 = mean; 
} 
void T_Tsunami::setSigma (const double sigma) 
{ 
    p2 = sigma; 
} 
const double T_Tsunami::getNorm() const 
{ 
    return p0; 
} 
const double T_Tsunami::getMean() const 
{ 
    return p1; 
} 
const double T_Tsunami::getSigma() const 
{ 
    return p2; 
} 


//THIS IS WHAT MY VIRTUAL FUNCTION "TOESTAND" SHOULD DO FOR THIS DERIVED CLASS 
const vector<double> BasisToestand::Toestand(int GOLF) const    
{       
    vector<double>T_1; 
    for(int i = 0; i < GOLF; i++) 
     { double y = 0.25*(1-tanh(double(i-75)/5)); 
      T_1.push_back(y); 
     } 
    cout<< "Creating vector T_1" << endl; 
    return T_1; 
} 

主要功能:

#include <iostream> 
using std::cout; 
using std::endl; 
using std::scientific; 

#include <string> 
using std::string; 

#include <cmath> 

#include <iomanip> 
using std::setw; 
using std::setprecision; 

#include <vector> 
using std::vector; 


#include "BasisToestand.h" 
#include "T_Tsunami.h" 


int main() 
{ 

    T_Tsunami myTsunami_1; 
    BasisToestand *funPtr1 = &myTsunami_1; 
    BasisToestand& funRef1 = myTsunami_1; 

    cout << "value at x=0 using pointers is " << funPtr1->getVal(0.) << endl; //WORKS 
    cout << "value at x=0 using references is " << funRef1(0.) << endl;  //WORKS 

    cout << "Testing Tsunami" **<<** funPtr1->Toestand(10) << endl;  

    //THIS DOES NOT WORK, the bold thing is where I get the error. 

    return 0; 
} 
+0

T_Tsunami_H需要包含basictoestand.h –

+0

爲什麼你的Toestand成員註釋掉?這不應該編譯,不會:http://coliru.stacked-crooked.com/a/510c682db9dd696c –

+1

你的問題從[最小,完全,可覈查示例]將受益(http://stackoverflow.com/幫助/ MCVE)。請創建一個,這樣我們可以更好地幫助你。你甚至可以在創建時自己找到答案。 – chris

回答

3

你的問題可以減少到這一點:

#include <vector> 
#include <iostream> 
int main() { 
    std::vector<int> v; 
    std::cout << v; 
} 

main.cpp:5:19: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'std::vector')

而且這個錯誤非常說明問題。您不能簡單地通過operator <<將容器發送到流。您可以使用該有人寫這樣做(Pretty-print C++ STL containers)功能,或者乾脆環比向量的內容和自己做。

cout << "Testing Tsunami "; 
const vector<double>& Toestand = funPtr1->Toestand(10); //get a reference to the vector 
for(int i=0; i<Toestand.size(); ++i) //for each element 
    cout << Toestand[i] << ','; //print the element and a comma 
cout << endl;  
+0

我對不必要的長時間遺憾碼,這是所有很新的我..我種得到了什麼現在是錯誤的,所以感謝您的(雖然我並不確切地知道你所說的「容器」的意思),但我不完全明白我應該做修復它..很抱歉,.. – user3600130

+0

@ user3600130:「容器」是簡單地保存數據「元素」的對象。一些容器是數組,向量,列表,地圖,集合,deques和其他。 –

相關問題