头文件:
#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文件:
#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++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\main.cpp||
In function 'int main()':| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\main.cpp|9|error:
no matching function for call to 'Contmem::Contmem()'|
C:\C++ CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|8|note: candidate:
Contmem::Contmem(int, int, int)| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|8|note: candidate
expects 3 arguments, 0 provided| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|5|note: candidate:
Contmem::Contmem(const Contmem&)| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|5|note: candidate
expects 1 argument, 0 provided| ||=== Build failed: 1 error(s), 0
warning(s) (0 minute(s), 0 second(s)) ===|
答案 0 :(得分:6)
您缺少类的默认构造函数。你只有这个
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();
}
你试图构造一个新的Contmem
对象而不传递这3个参数
实际上,那些编译器错误告诉你真正的问题是什么。
答案 1 :(得分:5)
prepare(for segue: UIStoryboardSegue, sender: Any?)
尝试调用默认构造函数const Contmem obj;
。
但因为:
Contmem()
有一个mem-initializer和一个Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
成员,你的默认构造函数是 deleted。
因此,编译器无法将该语句与任何现有构造函数匹配,因为只存在mem-initializer构造函数。