簡體   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