We are using an custom image class written by our professor. I can load images like this
SimpleGrayImage in("images/uni01.pgm");
Now I have a function that looks like this
SimpleGrayImage apply_mask(SimpleGrayImage &img,SimpleFloatImage &mask){
and I can also uses methods on it like this apply_mask(...).show();
But I am not allowed to do this SimpleGrayImage img = apply_mask(...);
Did I miss something or could it be that our professor forgot to add another constructor?
test.cpp:133:43: error: no matching function for call to ‘SimpleGrayImage::SimpleGrayImage(SimpleGrayImage)’
SimpleGrayImage img = apply_mask(in,mask);
^
test:133:43: note: candidates are:
In file included from SimpleFloatImage.h:13:0,
from aufgabe11_12_13.cpp:13:
SimpleGrayImage.h:110:2: note: SimpleGrayImage::SimpleGrayImage(const string&)
SimpleGrayImage(const std::string& filename);
^
SimpleGrayImage.h:110:2: note: no known conversion for argument 1 from ‘SimpleGrayImage’ to ‘const string& {aka const std::basic_string<char>&}’
SimpleGrayImage.h:91:2: note: SimpleGrayImage::SimpleGrayImage(SimpleGrayImage&)
SimpleGrayImage(SimpleGrayImage &img);
^
SimpleGrayImage.h:91:2: note: no known conversion for argument 1 from ‘SimpleGrayImage’ to ‘SimpleGrayImage&’
SimpleGrayImage.h:86:2: note: SimpleGrayImage::SimpleGrayImage(int, int)
SimpleGrayImage(int wid, int hig);
^
SimpleGrayImage.h:86:2: note: candidate expects 2 arguments, 1 provided
SimpleGrayImage.h:80:2: note: SimpleGrayImage::SimpleGrayImage()
SimpleGrayImage();
^
SimpleGrayImage.h:80:2: note: candidate expects 0 arguments, 1 provided
From the errors i can see that the copy constructor is not defined correctly:
SimpleGrayImage::SimpleGrayImage(SimpleGrayImage&)
This will not be called since the value you return from function apply_mask is not local variable but an rvalue.
For the constructor to take rvalues you need to change the signature of the copy constructor to
SimpleGrayImage::SimpleGrayImage(const SimpleGrayImage&)//note the new const
Edit: For more information on rvalues you can check this link. Rvalues can be converted to const SimpleGrayImag& a = apply_mask() because the const ensures that you can't make changes to a so the compiler can safely give you the address of that local memory. The definition without const can only be used on lvalues like such:
SimpleGrayImag a;//this is an lvalue;
SimpleGrayImag b = a;
The best approach is to use const on all the arguments that you don't want them to be output arguments. Also you can make functions with both versions const and non const and most of the time the compiler will understands you, however it should be avoided because the code is harder to read and understand.
Related
I have the struct student and I did not declare a constructor. What will happen if I do the following?
struct student{
int assns, mt, finalExam;
float grade(){…}
}
student billy (60, 70, 80);
This answer is written according to the question heading, and not the body, as they seem to be gravely conflicting, hope the OP edits this.
You will encounter a error during compile time.
Code:
#include <iostream>
class test
{
int tt;
};
int main ()
{
test t1 (34);
}
Compiler Error:
In function 'int main()':
10:17: error: no matching function for call to 'test::test(int)' 10:17: note: candidates are:
2:7: note: test::test()
2:7: note: candidate expects 0 arguments, 1 provided
2:7: note: constexpr test::test(const test&)
2:7: note: no known conversion for argument 1 from 'int' to 'const test&'
2:7: note: constexpr test::test(test&&)
2:7: note: no known conversion for argument 1 from 'int' to 'test&&'
This happens as there is no constructor defined which takes a parameter. Without the ctor there is no meaning of class, as you can never initialize its data member, and how can you expect something to be constructed if the construction company itself is absent.
The compiler will throw error.
I'm trying to understand the meaning of the errors that we generally face in out C++ programs.
While compiling a program I got a error (I did this error intentionally, please don't tell that how to correct that) and there a note is present 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 is :
#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 haven't defined a constructor which can take one argument and doing that will remove my error.
Okay, while 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 is meaning of last line file1.cpp:2:7: note: no known conversion for argument 1 from ‘int’ to ‘const account&’ ?
1) You already know that you don't have an constructor that takes an int.
2) You know that you are trying to construct account with an int.
3) If you don't do it, compilers will create default copy-constructors, assignment-operators
4) The default copy-constructor takes a const reference to account
So what happens here? As there is only a default-constructor and you are constructing with one parameter the compiler thinks you want to copy-construct. As you are giving him an int as parameter for the copy-constructor, the compiler tries to convert the int to an account - which doesn't work, and he tells you about it: "no conversion possible from int to account"
This is very important to know as this is a source of many bugs. You propably didn't want to call the copy-constructor. But what happens if the compiler really finds a way to convert the type you used as a parameter to account? A mess....
I want to know what is meaning of last line file1.cpp:2:7: note: no known conversion for argument 1 from ‘int’ to ‘const account&’ ?
First, the message tell you
no matching function for call to ‘account::account(int)’
And there're two candidates, the 1st is the default ctor, but
file1.cpp:7:9: note: candidate expects 0 arguments, 1 provided
The 2nd is the copy ctor (implicitly generated), and its parameter's type is const account&, but
file1.cpp:2:7: note: no known conversion for argument 1 from ‘int’ to ‘const account&’
class constructor is a simple function as like as others, so when you send a int type to the function as parameter, you need to define parameter type for function :
class account
{
private:
int a_no;
public:
account(int a){ a_no = a; }
};
now, when you type account a2(2); int type defined for constructor and there isn't any problem
I am currently making a big integer library for my college assignment.
I am experiencing a problem with the following code:
MyInteger::MyInteger(){ //default
//some code
}
MyInteger::MyInteger(char *s){ //construct from string
//some code
}
MyInteger::MyInteger(MyInteger &otherInt){ //copy constructor
//some code
}
MyInteger MyInteger::parse(char *s){ //parse string and create new object
return MyInteger(s);
}
I get the following error about the parse function:
MyInteger.cpp: In static member function ‘static MyInteger MyInteger::parse(char*)’:
MyInteger.cpp:34:20: error: no matching function for call to ‘MyInteger::MyInteger(MyInteger)’
return MyInteger(s);
^
MyInteger.cpp:34:20: note: candidates are:
MyInteger.cpp:23:1: note: MyInteger::MyInteger(MyInteger&)
MyInteger::MyInteger(MyInteger &otherInt){ //copy constructor
^
MyInteger.cpp:23:1: note: no known conversion for argument 1 from ‘MyInteger’ to ‘MyInteger&’
MyInteger.cpp:10:1: note: MyInteger::MyInteger(char*)
MyInteger::MyInteger(char *s){ //construct from string
^
MyInteger.cpp:10:1: note: no known conversion for argument 1 from ‘MyInteger’ to ‘char*’
MyInteger.cpp:4:1: note: MyInteger::MyInteger()
MyInteger::MyInteger(){ //set string to 0
^
MyInteger.cpp:4:1: note: candidate expects 0 arguments, 1 provided
Shouldn't it be using the 2nd constructor?
Either it is confusing the string with a MyInteger, or the string is being converted to a MyInteger somehow and then the compiler is trying to convert it again using the 3 candidates that it has listed. A similar error is occuring with the overloaded + operator.
Please tell me what I am doing wrong.
It's not the MyInteger(s) that's the problem. It constructs that temporary object. It's the attempt to return this temporary object that is the problem. You are returning by value, which means that a copy needs to be made, yet your copy constructor takes a MyInteger&, which is unable to bind to temporary objects (rvalues). Your copy constructor should have parameter of type const MyInteger& instead, which will allow it to do so.
I'm working in C++ with a Protocol Buffer template including the following message:
message StringTable {
repeated bytes s = 1;
}
I'm attempting to add a new value to the existing data, like so:
pb.stringtable().s().Add(replace_key);
However, this generates an error on compilation (using clang on OS X):
test.cpp:51:4: error: member function 'Add' not viable: 'this' argument
has type 'const ::google::protobuf::RepeatedPtrField< ::std::string>',
but function is not marked const
pb.stringtable().s().Add(replace_key);
^~~~~~~~~~~~~~~~~~~~
Any clues? I'm very much a C++ newbie so may be making a dumb error.
Edit:
Using the accessors produces a similar error:
pb.stringtable().add_s(replace_key);
results in:
test.cpp:51:21: error: no matching member function for call to 'add_s'
pb.stringtable().add_s(replace_key);
~~~~~~~~~~~~~~~~~^~~~~
./osmformat.pb.h:3046:26: note: candidate function not viable: 'this' argument has type 'const ::StringTable', but method is not marked const
inline void StringTable::add_s(const ::std::string& value) {
^
./osmformat.pb.h:3050:26: note: candidate function not viable: 'this' argument has type 'const ::StringTable', but method is not marked const
inline void StringTable::add_s(const char* value) {
^
./osmformat.pb.h:3043:36: note: candidate function not viable: requires 0 arguments, but 1 was provided
inline ::std::string* StringTable::add_s() {
^
./osmformat.pb.h:3054:26: note: candidate function not viable: requires 2 arguments, but 1 was provided
inline void StringTable::add_s(const void* value, size_t size) {
Problem solved.
The existing StringTable isn't mutable by default. However, using the mutable_ accessors makes it so:
pb.mutable_stringtable().add_s(replace_key);
I am confused by the following behavior:
My class "Application" adds in the constructor elements of the type "SplashScreen" (derived from my class "Screen " which is the container type) using ptr_map_insert into a pointer container of the type Screen. Example:
boost::assign::ptr_map_insert<SplashScreen>(screenContainer_)(GameScreens::Splash, curWindow_, curFileSystem_, curInputManager_);
According to the documentation of ptr_map_insert, the last pair of brackets begins with the key and the following arguments are passed to the constructor of the SplashScreen class.
curWindow_ etc. are non-const private members of my class "Application"
I don't know why, but GCC reports an error because the arguments passed to the constructor are const references and the constructor of SplashScreen needs regular references.
SplashScreen(sf::RenderWindow& curWindow, System::FileSystem& curFileSystem, System::InputManager& curInputManager);
The complete error message is below and partially translated by me because it is/was in german.
/usr/include/boost/preprocessor/iteration/detail/local.hpp: In Elementfunktion »boost::assign::ptr_map_inserter<PtrMap, Obj>& boost::assign::ptr_map_inserter<PtrMap, Obj>::operator()(const T&, const T0&, const T1&, const T2&) [with T = Oxid::GameScreens::gameScreenEnum, T0 = sf::RenderWindow, T1 = Oxid::System::FileSystem, T2 = Oxid::System::InputManager, PtrMap = boost::ptr_map<Oxid::GameScreens::gameScreenEnum, Oxid::Screen>, Obj = Oxid::Game::SplashScreen, boost::assign::ptr_map_inserter<PtrMap, Obj> = boost::assign::ptr_map_inserter<boost::ptr_map<Oxid::GameScreens::gameScreenEnum, Oxid::Screen>, Oxid::Game::SplashScreen>]«:
/blabla/main/application.cpp:42:132: instanced(?) from here
/usr/include/boost/preprocessor/iteration/detail/local.hpp:43:1: Error: no matching function for calling »Oxid::Game::SplashScreen::SplashScreen(const sf::RenderWindow&, const Oxid::System::FileSystem&, const Oxid::System::InputManager&)«
/usr/include/boost/preprocessor/iteration/detail/local.hpp:43:1: Anmerkung: candidates are :
../include/splashscreen.h:16:17: Anmerkung: Oxid::Game::SplashScreen::SplashScreen(sf::RenderWindow&, Oxid::System::FileSystem&, Oxid::System::InputManager&)
../include/splashscreen.h:16:17: Anmerkung: no known conversion for argument 1 from »const sf::RenderWindow« to »sf::RenderWindow&«
../include/splashscreen.h:13:15: Anmerkung: Oxid::Game::SplashScreen::SplashScreen(const Oxid::Game::SplashScreen&)
../include/splashscreen.h:13:15: Anmerkung: candidate requires 1 Argument, 3 denoted
The boost sourcecode doesn't indicate that arguments are changed to const or something similar. What did I overlook that this conversion occurs?
Edit: Just viewed the actual boost changelog (I am using 1.48.0) but they don't contain something about this problem.
Regards
As I look at the source it appears that it does take the parameters by const reference, presumably so that in most cases you don't have loss-of-const concerns. I believe that in your case you're going to have to use insert directly on the ptr_map instead of the helper template.