2010-10-31 31 views
0

编辑:我使用不当#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++新手。

回答

1

stdio.h适用于C功能,如printfscanf

如果你想使用C++风格的I/O(流),你需要包括iostream

+0

Voila!修复了这个问题。这就是我将C风格的代码转换为C++所得到的结果,没有足够的考虑。 – Trevor 2010-10-31 17:31:52

相关问题