当我调用 main 中使用 const 对象的 const 函数时,不断出现错误
keep getting error when i call a const function with a const object in main
头文件:
#ifndef CONTMEM_H
#define CONTMEM_H
class Contmem {
public:
Contmem(int a, int b, int c);
int total;
int mem;
const int constmem;
int printconst() const;
const int constant;
void print();
};
#endif // CONTMEM_H
Contmem.cpp file:
#include "Contmem.h"
#include <iostream>
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
{
total = mem * constmem * constant;
}
void Contmem::print()
{
std::cout << "This is my variable member " << mem << " and this is my constmem member " << constmem << "with the constant member" << constant << std::endl;
}
int Contmem::printconst() const
{
return total;
std::cout << "This is the total variable " << total << std::endl;
}
主要功能:
#include <iostream>
#include "Contmem.h"
int main()
{
Contmem cont(3, 4, 5);
cont.print();
const Contmem obj;
obj.printconst();
}
错误文件:
|=== Build: Debug in CONST_&_MEMBER_INITIALIZER
(compiler: GNU GCC Compiler) ===| C:C++
CODEBLOCKCONST_&_MEMBER_INITIALIZERmain.cpp||
In function 'int main()':| C:C++
CODEBLOCKCONST_&_MEMBER_INITIALIZERmain.cpp|9|error:
no matching function for call to 'Contmem::Contmem()'|
C:C++ CODEBLOCKCONST_&_MEMBER_INITIALIZERContmem.h|8|note: candidate:
Contmem::Contmem(int, int, int)| C:C++
CODEBLOCKCONST_&_MEMBER_INITIALIZERContmem.h|8|note: candidate
expects 3 arguments, 0 provided| C:C++
CODEBLOCKCONST_&_MEMBER_INITIALIZERContmem.h|5|note: candidate:
Contmem::Contmem(const Contmem&)| C:C++
CODEBLOCKCONST_&_MEMBER_INITIALIZERContmem.h|5|note: candidate
expects 1 argument, 0 provided| ||=== Build failed: 1 error(s), 0
warning(s) (0 minute(s), 0 second(s)) ===|
缺少类的默认构造函数。你只有这个
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
{
total = mem * constmem * constant;
}
但在这里
int main()
{
Contmem cont(3, 4, 5);
cont.print();
const Contmem obj; // <--here
obj.printconst();
}
您正在尝试在不传递这 3 个参数的情况下构造一个新的Contmem
对象
实际上,这些编译器错误告诉您真正的问题是什么。
const Contmem obj;
尝试调用默认构造函数Contmem()
。
但因为:
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
具有 mem 初始值设定项和const
成员,则默认构造函数将被删除。
因此,编译器无法将该语句与任何现有构造函数匹配,因为仅存在 mem 初始值设定项构造函数。
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 类型为 "Bucket&"(未限定的 const 限定)的引用不能使用 "SortedList." 类型的值进行初始化 如何修复此错误?
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 为什么我的编译器在调用 const getter 函数时抛出"转换丢失(const)限定符"错误?
- C++ 错误:由于 swprintf 而从 'int' 到 'const wchar_t* 的转换无效
- test1.cpp:9:77:错误:对"(const std::normal_distribution) <double>(std::mt19937&)"的调用不匹配
- 错误:对类型 'const ItemInstance' 的引用无法绑定到类型 'void' 的右值
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- C++ Boost - 序列化错误 - 将"const B"作为"this"参数
- 错误:从 'char' 到 'const char' [-允许] strcat(加密,密钥[i])的转换无效;
- 索引 2D 向量时'Reference to type const..'编译错误
- 引发"char const*"错误的实例后调用的终止
- 在返回 const 错误的函数中传递值
- 在c++包装的C代码中重复const错误
- 静态常量匿名联合使用 GCC 4.9.3 给出"uninitialized const"错误
- 将const char*传递给方法,但得到char const *错误
- 在' const '错误之前预期的主表达式