简体   繁体   English

Main无法调用函数:在此范围内未声明函数

[英]Main can not call a function: function was not declared in this scope

I am in the process of changing a C file to a C++ file (to eventually integrate it with a C program). 我正在将C文件更改为C ++文件(最终将其与C程序集成)。 I'm very new to C++ and in fact this is my first exposure to it. 我是C ++的新手,实际上这是我第一次接触C ++。 I have a test.cpp file that declares function main and hello as follows: 我有一个test.cpp文件,它声明函数main和hello如下:

#include "test.h"

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

void hello()
{
    std::cout << "Hello there!" << endl;
}

The test.h file is declared as follows: test.h文件的声明如下:

#include <iostream>

extern "C" void hello();

When I compile the program using g++ test.cpp, I get the error "hello was not declared in this scope". 当我使用g ++ test.cpp编译程序时,出现错误“未在此范围内声明hello”。

Any suggestions? 有什么建议么?

Also, where does one find the API for C++ classes and their functions? 另外,在哪里可以找到C ++类及其功能的API?

I think that you may have misread the error message. 我认为您可能误读了错误消息。 The only mistake that should cause an error is the fact that you haven't qualified endl with std:: . 唯一会导致错误的错误是您还没endl std::限定endl的事实。 Are you sure that the error message wasn't about endl ? 您确定错误消息与endl无关吗?

Compiling your complete test case, I get the following: 编译完整的测试用例,我得到以下信息:

$ g++ test.cpp
test.cpp: In function ‘void hello()’:
test.cpp:11:37: error: ‘endl’ was not declared in this scope
      std::cout << "Hello there!" << endl;
                                     ^
test.cpp:11:37: note: suggested alternative:
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from test.h:1,
                 from test.cpp:1:
/usr/include/c++/4.8/ostream:564:5: note:   ‘std::endl’
     endl(basic_ostream<_CharT, _Traits>& __os)
     ^

Fixing the error by adding the std:: to endl fixes all compile and link errors and gives hello C language linkage as expected. 通过在endl添加std::来修复错误,可修复所有编译和链接错误,并按预期提供hello C语言链接。

(Note, there is no harm - and it's probably even clearer - to add extern "C" to the definition of the function hello , but it is not necessary so long as the first visible declaration declares the correct language linkage.) (请注意,在函数hello的定义中添加extern "C"并没有什么危害,而且甚至更清楚了,但是没有必要,只要第一个可见声明声明正确的语言链接即可。)

You should remove extern "C" altogether, 您应该extern "C"删除extern "C"
Use the standard namespace. 使用标准名称空间。

But if you must compile the function as extern "C" , it is not required you put it before the function definition, only the declaration, which you have done. 但是,如果必须将函数编译为extern "C" ,则不需要将其放在函数定义之前,只需将其放在声明之前即可。
But if you'd like to add it to both declaration and definition, it would be fine for you to do so. 但是,如果您想将其添加到声明和定义中,则可以这样做。

Example: 例:

#include "test.h"

using namespace std;    

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

void hello()
{
    cout << "Hello there!" << endl;
}

问题是您在包含文件中声明了extern "C" ,但是它在hello.cpp源文件中,因此它将被编译为c ++,而不是c。

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

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