繁体   English   中英

为什么我的C ++代码无法显示cout字符串

[英]why cant my c++ code show cout string

这是我的main.cpp

#include <iostream>
using namespace std;
#include "test.h"

void main()
{
    Solution aa;
    aa.aaa();

    cin.get();
}

这是我的测试。

#pragma once

class Solution {
public:
    void aaa() {};
};

这是我的test.cpp

#include <iostream>

using namespace std;

class Solution {
public:  

    void aaa() {
        cout << "aaaaa" << endl;
    };
};

当我运行c ++代码时,它什么也没显示。 谁能告诉我为什么?

您的test.h使用绝对不做任何事情的aaa()类方法声明并定义了Solution类。

 void aaa() {};

看到, aaa()方法什么也不做。

您的main()称为此方法,它绝对不执行任何操作。

最终结果:未发生任何意外。

您还碰巧有一个test.cpp ,它恰好声明并定义了自己的同名类。 至少,这是未定义的行为,我会对所有决定将main.cpptest.cpp链接在一起都没有错的C ++编译器+链接器大加test.cpp

在test.h中:

void aaa() {};

请注意,您在()后面放了空括号,而不仅仅是分号。 如果要编写函数声明,则只需编写函数名称和参数,而不要括号。 现在,编译器认为您想编写一个不执行任何操作的函数。 (我很惊讶这没有引起链接错误)

您的test.htest.cpp文件实施不正确。 他们应该看起来像这样:

测试

#pragma once

class Solution {
public:
    void aaa();
};

测试文件

#include "test.h"
#include <iostream>

using namespace std;

void Solution::aaa() {
    cout << "aaaaa" << endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM