我一直有兩天這個相同的錯誤:直到現在我的代碼的最佳版本是在下面,編譯器不斷抱怨說「沒有操作符< <匹配這些操作數「,雖然我做了#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;
}
T_Tsunami_H需要包含basictoestand.h –
爲什麼你的Toestand成員註釋掉?這不應該編譯,不會:http://coliru.stacked-crooked.com/a/510c682db9dd696c –
你的問題從[最小,完全,可覈查示例]將受益(http://stackoverflow.com/幫助/ MCVE)。請創建一個,這樣我們可以更好地幫助你。你甚至可以在創建時自己找到答案。 – chris