c++ - ‘operator<<’ 中的 ‘os << "不匹配 当 os 是 ostream &

标签 c++

编辑:我错误地使用了 #include <stdio.h>当我需要包括 <iostream> .发生这种情况主要是因为我将一些 C 风格的代码与一个更大的 C++ 程序集成在一起。

结束编辑

我正在为我的大学类(class)使用一个简单的测试框架,但每当我使用提供的预处理器宏时,我都会遇到这两个错误。我自己扩展了宏,试图找出我做错了什么,但我很困惑。

错误:

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 */

源文件/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;
}

抱歉,行太长了,这就是宏扩展的目的。哦,如果有帮助的话,传递给 Test() 的是 cout,如

Url url = Url();
url.Test(std::cout);

所以,我很困惑,如果这看起来是个愚蠢的问题,抱歉。我是 C++ 新手。

最佳答案

stdio.h 用于 C 函数,例如 printfscanf

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

关于c++ - ‘operator<<’ 中的 ‘os << "不匹配 当 os 是 ostream &,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4064088/

相关文章:

c++ - C中的嵌入式编程

c++ - 抛出异常并从函数返回结果

c++ - C++ 11 中的数组

命名空间中的 C++ 函数不起作用?

c++ - Crypto++ 和压缩 EC key

c++ - 手动控制串行端口引脚(TxD 和 RxD)

c++ - 元编程和 SFINAE/std::enable_if:无限模板递归

c++ - 将 FFMPEG 帧写入 png/jpeg 文件

c++ - 为什么在此代码中出现运行时错误SIGSEGV

c++ - 更改 STL 映射的赋值