編輯:我使用不當#include <stdio.h>
當我需要包括<iostream>
。這主要是因爲我將一些C風格的代碼與更大的C++程序集成在一起。敵不過「運算符<<」在「OS <<‘測試失敗[’」當OS是一個ostream&
EndEdit中
我用一個簡單的測試框架,我的大專班,但每當我使用所提供的預處理器宏我得到這兩個錯誤。我自己擴展了這個宏,試圖弄清楚我做錯了什麼,但我很難過。
錯誤:
src/Url.cpp: In member function ‘bool Url::Test(std::ostream&)’:
src/Url.cpp:35: error: no match for ‘operator<<’ in ‘os << "Test Failed ["’
src/Url.cpp:35: error: ‘endl’ was not declared in this scope
INC/Url.h
#ifndef _URL_H
#define _URL_H
using namespace std;
class Url {
public:
Url();
Url(const Url& orig);
virtual ~Url();
bool Test(ostream & os);
bool setAsUrl(string relOrRegUrl, string baseUrl);
bool hasUrl();
string getUrl();
bool isHtml();
private:
string fullUrl;
bool html;
};
#endif /* _URL_H */
的src/Url.cpp
#include <string>
#include <cstring>
#include <stdio.h>
#include "UnitTest.h" //This contains the macro
#include "Url.h"
//using namespace std;
Url::Url() {
fullUrl = "NULL";
html = false;
}
Url::Url(const Url& orig) {
}
Url::~Url() {
}
bool Url::Test(ostream & os) {
bool success = true;
Url url= Url();
url.setAsUrl("http://www.cnn.com/news.jpg","http://www.cnn.com");
do {
if (!(url.isHtml() == false)) {
success = false; os << "Test Failed [" << __FILE__ << ", " << __LINE__ << "]" << endl; //line 35
}
}while(false);
// TEST(url.isHtml() == false); this is what gets expanded to the above
return success;
}
bool Url::setAsUrl(string relOrRegUrl, string baseUrl){
//Lots of code irrelevant to the question
}
bool Url::hasUrl(){
return fullUrl == "NULL";
}
string Url::getUrl(){
return fullUrl;
}
bool Url::isHtml(){
return html;
}
很抱歉的長線路長度,這就是該宏擴展爲。哦,如果有幫助,什麼東西被傳遞到測試()被清點在
Url url = Url();
url.Test(std::cout);
所以,我很爲難,如果這似乎是一個愚蠢的問題,對不起。我是C++新手。
Voila!修復了這個問題。這就是我將C風格的代碼轉換爲C++所得到的結果,沒有足夠的考慮。 – Trevor 2010-10-31 17:31:52