What does this mean ?: note: unknown conversion for argument 1 of 'int to' const account &

I am trying to understand the meaning of the errors that we usually encounter in C ++ programs.

When compiling the program, I got an error (I made this error intentionally, please do not say how to fix it), and there is a note there, which is:

note: no known conversion for argument 1 from 'int' to 'const account&' 

I want to understand the meaning of this note.

My program:

 #include<iostream> class account { private: int a_no; public: account() { a_no = 0; } void showData() { std::cout<<"\n account number = "<<a_no<<std::endl; } }; int main() { account a1; a1.showData(); account a2(2); a2.showData(); return 0; } 

I know that I have not defined a constructor that can take one argument, and that will make my mistake.

Well, compiling this, I got:

 file1.cpp: In function 'int main()': file1.cpp:20:17: error: no matching function for call to 'account::account(int)' account a2(2); ^ file1.cpp:20:17: note: candidates are: file1.cpp:7:9: note: account::account() account() ^ file1.cpp:7:9: note: candidate expects 0 arguments, 1 provided file1.cpp:2:7: note: account::account(const account&) class account ^ file1.cpp:2:7: note: no known conversion for argument 1 from 'int' to 'const account&' 

I want to know what the last line of file1.cpp:2:7: note: no known conversion for argument 1 from 'int' to 'const account&' means file1.cpp:2:7: note: no known conversion for argument 1 from 'int' to 'const account&' ?

+5
source share
3 answers

1) You already know that you do not have a constructor that accepts an int. 2) You know that you are trying to create an account using int. 3) If you do not, the compilers will create default copy constructors, assignment operators 4) The default instance constructor accepts a const reference to the account

And what is going on here? Since there is only a default constructor, and you are building with one parameter, the compiler believes that you want to copy-construct. Since you give it an int as a parameter for copy-constructor, the compiler tries to convert int into an account - this does not work, and he tells you about it: "conversion from int to account is impossible"

This is very important to know, as it is the source of many errors. You might not want to call a copy constructor. But what happens if the compiler really finds a way to convert the type that you used as the parameter for the account? The mess ...

+3
source

I want to know what the last line of file1.cpp means: 2: 7: note: unknown conversion for argument 1 of 'int to' const account &

First, a message tells you

there is no corresponding function to call the account account (int)

And there are two candidates, the first by default, but

file1.cpp: 7: 9: note: candidate expects 0 arguments, 1 provided

The second instance is ctor (implicitly generated), and its parameter type is const account& , but

file1.cpp: 2: 7: note: unknown conversion for argument 1 of 'int to' const account &

+1
source
Class constructor

is a simple function, like others, so when you send an int type to a function as a parameter, you need to determine the type of parameter for the function:

 class account { private: int a_no; public: account(int a){ a_no = a; } }; 

now when you enter account a2(2); int type defined for constructor and there is no problem

0
source

Source: https://habr.com/ru/post/1239664/


All Articles