visual-studio 当您收到类声明的编译错误“看起来像函数定义”时,这意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/684715/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean when you get a compile error "looks like a function definition" for a class declaration?
提问by Tim Keating
I recently encountered this problem. I found many instances of people asking the question—here, for example—but no concrete answers.
我最近遇到了这个问题。我发现很多人问这个问题的例子——例如在这里——但没有具体的答案。
Here's the sample code hoisted from that link:
这是从该链接中提取的示例代码:
class AFX_BASE_APPLICATION_APP_CLASS CFileExtension 
{
public:
   CFileExtension ();           
   virtual ~CFileExtension ();
};
The error this generates is:
这产生的错误是:
c:\FileExtension.h(14) : error C2470: 'CFileExtension' : looks like a function definition, but there is no formal parameter list; skipping apparent body
c:\FileExtension.h(14) : error C2470: 'CFileExtension' : looks like a function definition, but there is no formal parameter list; skipping apparent body
回答by paxdiablo
You've almost certainly missed the header which defines AFX_BASE_APPLICATION_APP_CLASS. In that case, it would be passed through unaltered and VC++ would assume that CFileExtensionwas a function that returned class AFX_BASE_APPLICATION_APP_CLASS.
您几乎肯定错过了定义AFX_BASE_APPLICATION_APP_CLASS. 在这种情况下,它会原封不动地通过,VC++ 会假设这CFileExtension是一个返回class AFX_BASE_APPLICATION_APP_CLASS.
And, since it thinks it's a function, it also thinks it needs parentheses.
而且,因为它认为它是一个函数,所以它也认为它需要括号。
You just need to find where AFX_BASE_APPLICATION_APP_CLASSis defined and #includethat file.
您只需要找到AFX_BASE_APPLICATION_APP_CLASS定义的位置和#include该文件。
回答by Pramod
I encountered the same compiler error. The problem was a missing colon
我遇到了同样的编译器错误。问题是缺少冒号
My code was something like:
我的代码是这样的:
int MyClass:doSomething() {
}
It should have been: (Note '::' instead of ':'
它应该是:(注意 '::' 而不是 ':'
int MyClass::doSomething() {
}
回答by Tim Keating
...and the answer, since I couldn't be bothered to make a login for any of those forums, but I already had an OpenID :-)
...以及答案,因为我懒得登录任何这些论坛,但我已经有了一个 OpenID :-)
In my case (and likely all the ones I found), the problem was that I was missing an #include for the header file that contained the definition for the macro preceding the class name. The macro is most likely used as a build-configuration switched way to turn on __declspec(dllexport).
在我的情况下(可能是我找到的所有那些),问题是我缺少头文件的#include,其中包含类名前面的宏定义。该宏最有可能用作打开 __declspec(dllexport) 的构建配置切换方式。
The way I discovered this was by going into the project properties, "C/C++" | Preprocessor and turning on "Generate Preprocessed File." When I looked at the preprocessed output, I discovered that the macro was included verbatim instead of being expanded.
我发现这一点的方法是进入项目属性,“C/C++” | 预处理器并打开“生成预处理文件”。当我查看预处理输出时,我发现宏是逐字包含的,而不是被扩展的。
回答by bayda
just define AFX_BASE_APPLICATION_APP_CLASS
只是定义 AFX_BASE_APPLICATION_APP_CLASS
回答by StarShine
Another possible cause is using a __declspec(dllimport) instead of __declspec(dllexport) or no declspec at all as a class / function prefix in Visual C++.
另一个可能的原因是使用 __declspec(dllimport) 而不是 __declspec(dllexport) 或根本没有 declspec 作为 Visual C++ 中的类/函数前缀。
If you mix this up, your class may also fail to get recognized as such and you get the same error.
如果您将其混为一谈,您的课程也可能无法被识别,并且您会收到相同的错误。
回答by zar
I had the same problem and none of the solutions worked. The problem as posted in OP was coming from including the class:
我遇到了同样的问题,但没有一个解决方案有效。在 OP 中发布的问题来自包括课程:
class AFX_EXT_CLASS CMyClass : public CObject
{
public  :
// ....
}
This was an export class from a dll which I want to use in a console exe. If I create the console application as 'using mfc' in the create wizard, then this error was not happening but that option results in creating a different structure of the app.
这是我想在控制台 exe 中使用的 dll 的导出类。如果我在创建向导中将控制台应用程序创建为“使用 mfc”,则不会发生此错误,但该选项会导致创建应用程序的不同结构。
Anyways I tried to include various headers so it knows AFX_EXT_CLASS, defined it but nothing worked. Long story short, the solution is that we have to enable to use MFC for the application for it to understand the MFC exported class from the dll. How we do it is by editing .vcprojfile and insert the line UseOfMFC="2"as shown below:
无论如何,我尝试包含各种标题,因此它知道AFX_EXT_CLASS,定义它但没有任何效果。长话短说,解决方案是我们必须为应用程序启用 MFC,以便它理解从 dll 导出的 MFC 类。我们的做法是通过编辑.vcproj文件并插入UseOfMFC="2"如下所示的行:
<Configurations>
    <Configuration
        Name="Debug|Win32"
        OutputDirectory="$(SolutionDir)$(ConfigurationName)"
        IntermediateDirectory="$(ConfigurationName)"
        ConfigurationType="1"
        UseOfMFC="2" <-------------Insert this line here
        CharacterSet="1"
Also #include <afx.h>to stdafx.h ( I included it at top right before the first include).
也#include <afx.h>到 stdafx.h (我在第一个包含之前将它包含在右上角)。
Now relead, clean and build the error should go away.
现在重新引导、清理和构建错误应该消失了。
There was no other way to change this option from project settings. This option is different from Project Properties >> Configuration Properties >> General >> 'Use of MFC'.
没有其他方法可以从项目设置中更改此选项。此选项不同于Project Properties >> Configuration Properties >> General >> 'Use of MFC'.
Update
更新
The UseOfMFC="2"trick maybe useful elsewhere but apparently if you just #include <afx.h>AND change the settings  project properties >> General >> 'Use of MFC'to Use MFC in shared dll, than this error goes away. If you don't change the later settings a compiler error will occur
该UseOfMFC="2"技巧可能在其他地方有用,但显然如果您只是#include <afx.h>将设置更改为  project properties >> General >> 'Use of MFC'在共享 dll 中使用 MFC,那么此错误就会消失。如果您不更改后面的设置,则会发生编译器错误
c:\program files (x86)\microsoft visual studio 8\vc\atlmfc\include\afx.h(24) : fatal error C1189: #error :  Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
Changing this actually also does set UseOfMFC="2"in the project file, so this is the right way.
更改它实际上也在UseOfMFC="2"项目文件中设置,所以这是正确的方法。

