Why is OnlineGDB ignoring the second argument in my member function? - c++

The function prototype in the class .h includes 2 arguments, and the function in the class .cpp includes the same 2 arguments, but the compiler pretends it only has 1 (ignoring the second) and prints the following error:
void list::swap(const string[] from, const string[] to)
^~~~
list.cpp:103:6: error: prototype for ‘void list::swap(const string*)’ does not match any in class ‘list’
void list::swap(const string[] from, const string[] to)
^~~~
In file included from list.cpp:6:0:
list.h:83:10: error: candidate is: void list::swap(const string*, const string*)
void swap(const string[], const string[]);
^~~~
I thought maybe it was a memory issue at first, but I have 26 GB of RAM and only 3GB being used by the web browser. I've tried refreshing and restarting my computer. You can see for yourself with this truncated code:
https://onlinegdb.com/-L_eUQTzY

The brackets have to come after the name:
void list::swap(const string from[], const string to[])
So it's assuming the first const string[] is the passed argument and ignoring everything else on that line because it expects either a ',' or '...' after the arguments name before something else is written.

Related

Cannot convert string to char* for argument 1

I am having an issue with my C++ code for converting a string of numbers in base b to another base n. My error is as follows:
cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘int base_to_decimal(char*, int)’
I am using g++ to compile, and I have been for a while, so I thought at first I knew what the issue was. It says that the method base_to_decimal accepts two arguments: one of type char* and one of type int. So all I should have to do to fix my issue is change the char* argument to a string, right?
Well, I looked, and the code for the function in question is:
int base_to_decimal(std::string input_base, int base)
So this method SHOULD be expecting a string, but for some reason when I pass in a string, it gets angry at me.
If someone could help me figure this out, that would be fantastic. I am using g++ 7.3.0, and running all this on Linux Mint 19.1.
(EDIT)
Main Method
Functions
Table
You've written a custom declaration on line 46 of your main:
int base_to_decimal(char * input_base, int base)
But in the functions file where you've defined your function, you have:
int base_to_decimal(std::string input_base, int base)
This is why you should not write in-line declarations for external functions, but instead put the declarations in a header file. #include in the file where the function is later defined, ensures that the definition matches the declaration that the other files are expecting. And likewise, it enforces that all those other files are trying to use the function the way it's actually coded.

GoogleMock and QString argument

I'm trying to use gmock with custom string type.
I have a method with QString argument which I want to mock:
MOCK_METHOD1(getValue, int(QString key));
I set an expectation:
EXPECT_CALL(mock, getValue("someKey"));
Got an error:
error: no matching function for call to 'MyMock::gmock_getValue(const char[8])'
include/gmock/gmock.h:9339:20: note: in definition of macro 'GMOCK_EXPECT_CALL_IMPL_'
((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
note: in expansion of macro 'EXPECT_CALL'
...
gmock/gmock.h:9730:7: note: no known conversion for argument 3 from 'const char [6]' to 'const testing::Matcher<const QString&>&'
gmock_##Method(GMOCK_MATCHER_(tn, F, 1) gmock_a1, \
But this works:
EXPECT_CALL(mock, getValue(QString("someKey")));
How can I use string arguments without wrapping each string literal with QString() ?
It is due to "someKey" is not a QString, it is a const char[8] as reported by the error and Google Test / Mock requires that the 2 classes are the same.
In the same way a compiler does not know if the value 10 is supposed to be int32 int64, uint32 or uint64, the same applies.

Issue with compiling a function which returns string &

A C++ function is as below: This is a auto generated code from Google Protocol Buffers. This is for a variable string NodeId in .proto file.
inline const ::std::string& TestClass::nodeId() const {
return *nodeId_;
The above function is called as below
const std::string& NodeId = TestClass.Testconfig().nodeId;
When I compile this file, i get below error.
prgms# g++ test.cpp
test.cpp:96:56: error: invalid initialization of reference of type ‘const string& {aka const std::basic_string<char>&}’ from expression of type ‘<unresolved overloaded function type>’
Can some body point me, what is wrong here and how to fix this particular error ?
You forgot to call the function:
const std::string& NodeId = TestClass.Testconfig().nodeId();
^^

C++ in Ruby C extensions, pointer problems

I'm tryign to build a Ruby C extension that uses some c++ libraries. Problem is I can't even get a simple "hello world" to work.
//hello_world.cpp
#include <ruby.h>
static VALUE tosCore;
static VALUE my_function( VALUE self )
{
VALUE str = rb_str_new2( "Hello World!" );
return str;
}
extern "C"
void Init_hello_world( void )
{
tosCore = rb_define_module("Core");
rb_define_module_function(tosCore, "my_method", my_function, 0);
}
The output I get is
compiling hello_world.cpp
hello_world.cpp: In function 'void Init_hello_world()':
hello_world.cpp:17:67: error: invalid conversion from 'VALUE (*)(VALUE) {aka lon
g unsigned int (*)(long unsigned int)}' to 'VALUE (*)(...) {aka long unsigned in
t (*)(...)}' [-fpermissive]
In file included from c:/Ruby200/include/ruby-2.0.0/ruby.h:33:0,
from hello_world.cpp:2:
c:/Ruby200/include/ruby-2.0.0/ruby/ruby.h:1291:6: error: initializing argument
3 of 'void rb_define_module_function(VALUE, const char*, VALUE (*)(...), int)'
[-fpermissive]
make: *** [hello_world.o] Error 1
I'm no C/C++ expert. Ruby is my language. I have compiled a few thousand lines of C++ under Rice with no problem, but since I want this particular extension to compile under Windows, Rice is not an option.
It's because the function callback you present to rb_define_module_function is not what the compiler expects. It want a function looking like this:
VALUE my_function(...)
But your function is
VALUE my_function( VALUE self )
Notice the difference in the argument list.
One way to get rid of the error, is to type cast the argument to the type that rb_define_module_function expects:
rb_define_module_function(tosCore, "my_method",
reinterpret_cast<VALUE(*)(...)>(my_function), 0);
You can read about reinterpret_cast here.

Why does the compiler tell me vector<string> is not declared?

I have some code here which reads from a file, and stores them in a vector.
I wish to pass this vector to another class. However, when i try to do that, it gives me a strange error, which i do not fully understand. It seems to be saying that the vector is not declared.
Here is the first few lines of a very long error:
g++ C_Main.cpp C_HomePage.cpp C_SelectionPage.cpp -o Project
C_HomePage.cpp:286:40: error: no ‘std::vector<std::basic_string<char> > HomePage::getDutiesList()’ member function declared in class ‘HomePage’
C_HomePage.cpp:290:26: error: ‘std::vector<std::basic_string<char> > HomePage::getResourcesList’ is not a static member of ‘class HomePage’
C_HomePage.cpp:290:26: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
C_HomePage.cpp:291:2: error: expected primary-expression before ‘return’
C_HomePage.cpp:291:2: error: expected ‘}’ before ‘return’
C_HomePage.cpp:291:2: error: in C++98 ‘HomePage::getResourcesList’ must be initialized by constructor, not by ‘{...}’
C_HomePage.cpp:291:2: error: no matching function for call to ‘std::vector<std::basic_string<char> >::vector(<brace-enclosed initializer list>)’
Here is line 282 - line 292 of C_HomePage.cpp
int HomePage::getInitPoints(){
return initPoints;
}
vector<string> HomePage::getDutiesList(){
return dutiesList;
}
vector<string> HomePage::getResourcesList{
return resourcesList;
}
Here is the corresponding declarations for those methods in H_HomePage.h
class HomePage {
//These values will be the property of the flat
//They are set before the login screen is displayed
string manager;
int initPoints;
vector<string> dutiesList;
vector<string> resourcesList;
vector<FlatMember> flatMemberList;
string loginName;
public:
HomePage(string);
void login(string);
string receivePassword();
void importFlatMembers(string);
void exportFlatMembers(string);
string getLoginName();
string getManager();
int getInitPoints();
vector<string> getDutiesList;
vector<string> getResourcesList;
};
I honestly does not know what is wrong, and have spent many hours getting frustrated over it already. Could someone please help?
You're missing parentheses in the declarations of getDutiesList and getResourcesList:
vector<string> getDutiesList();
vector<string> getResourcesList();
EDIT: You're also missing the parentheses in your .cpp file:
vector<string> HomePage::getResourcesList(){
return resourcesList;
}