【问题标题】:In C++, cout is not printing the output when a function is call from another file在 C++ 中,当从另一个文件调用函数时,cout 不会打印输出
【发布时间】:2022-07-30 03:08:33
【问题描述】:

这里我创建了三个文件,命名为,

  1. main.cpp(主文件)
#include"practicals.hpp"

using namespace std;

int main()
{
    firstPractical;
    return 0;
}
  1. practicals.hpp(自定义头文件)
#ifndef PRACTICAL_HPP
#define PRACTICAL_HPP
#pragma once
#include <iostream>

using namespace std;

void firstPractical();

#endif
  1. firstPractical.cpp(包含我的函数的文件)
#include <iostream>
#include "practicals.hpp"

using namespace std;

void firstPractical() {
    cout << "First Practical" << endl;
}

在这里,我使用 practicals.hpp 头文件从 main.cpp 文件中的 firstPractical.cpp 调用了“firstPractical”函数。因此,当我尝试运行该文件时,它应该在输出中打印“First Practical”。 Insted,它不会在输出中打印任何内容。

输出:

[Running] cd "d:\CD\CD Practicals\New folder\" && g++ main.cpp -o main && "d:\CD\CD Practicals\New folder\"main

[Done] exited with code=0 in 0.27 seconds

那么,我的代码有什么问题?

【问题讨论】:

    标签: c++ function printing output cout


    【解决方案1】:

    你忘记了函数名后面的括号

    int main()
    {
        firstPractical();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 2016-12-26
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多