C++:警告:C4930:未调用原型函数(是否打算定义变量?)

2023-12-03C/C++开发问题
19

本文介绍了C++:警告:C4930:未调用原型函数(是否打算定义变量?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个没有默认构造函数的类,我创建了一个变量而没有错误地提供参数,但不是一个很好的编译器错误,而是一个链接器错误,我不能't 找到导致它的代码行.

I have a class that does not have a default constructor, I created a variable without giving parameters by mistake, but instead of a nice compiler error, I got a linker error, where I couldn't find the line of code that was causing it.

最后,我设法找到了导致这种情况的代码,直到那时我才注意到我收到了这个警告:

In the end, I managed to find the code that caused this, and only then I noticed that I was getting this warning:

C++: warning: C4930: prototyped function not called (was a variable definition intended?)

奇怪的是当我更改代码时:

What's weird is when I changed the code from:

MyClass foo();

MyClass foo;

我确实遇到了编译器错误.

I did get a compiler error.

谁能给我解释一下为什么编译器突然开始奇怪了,是bug还是什么?

Can someone explain to me why the compiler suddenly started acting strange, is it a bug or something?

推荐答案

这个

MyClass foo();

是返回类型为 MyClass 且不接受参数的函数声明..

is a function declaration that has return type MyClass and does not accept arguments..

这个

MyClass foo;

是一个对象定义.由于您的类 MyClass 没有默认构造函数,因此编译器会发出错误.

is an object definition. As your class MyClass has no the default constructor the compiler issues an error.

这篇关于C++:警告:C4930:未调用原型函数(是否打算定义变量?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6