reference to non static member must be called [duplicate] - c++

This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 2 years ago.
Is there any way to declare the comp function here under the scope of the class declared like this.
class Solution {
public:
bool comp(vector<int> x, vector<int> y){
return x[0] < y[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),comp);
return {{}};
}
};
This code snippet gives error :
reference to non static member must be called

The compiler would say that a non-static member reference must be relative to a specific object. This hint states that you must need to create an object before accessing the class members if the members ain't declared as static.
There are two options here:
Either Declare the class member function as static and then directly access.
OTOH, create an object (instance) of the class and then access the class member function.
The first's solution would look something like:
class Solution {
public:
// Declared as 'static', so you don't need to create any instances
static bool comp(vector<int> x, vector<int> y) {
// ...
}
};
// ...
Solution::comp(...);
And the second solution would be (perhaps you didn't want this one):
class Solution {
public:
bool comp(...) {
// ...
}
};
// ...
Solution s;
s.comp(...);

Related

Placing class constructor [duplicate]

This question already has answers here:
Why does decltype not see the member declaration? [duplicate]
(3 answers)
Closed 5 years ago.
Why this code is incorrect?
class Method
{
public:
Method(decltype(info2) info1);
virtual ~Method(){}
protected:
QSharedPointer<info> info2;
};
But this code is correct:
class Method
{
public:
virtual ~Method(){}
protected:
QSharedPointer<info> info2;
public:
Method(decltype(info2) info1);
};
why place of class constructor is important?
I thought that place of definition class constructor isnt important.
I believe this part of the standard is relevant [basic.scope.class]/1.1:
The potential scope of a name declared in a class consists not only of the declarative region following the
name’s point of declaration, but also of all function bodies, default arguments,
exception-specification
s,
and
brace-or-equal-initializers
of non-static data members in that class (including such things in nested
classes).
Note that it only mentions default arguments. So this works since the decltype is referred in a default argument:
Method(QSharedPointer<int> info1 = decltype(info2)())
And this also works since it's inside a body:
Method(<...>)
{
decltype(info2) info3;
}
However your example does not work because such a placement of a decltype is not covered by the paragraph I quoted, thus the name info2 is considered out of scope.
Place of QSharedPointer info2;
is important.
'info2' should be defined before using it into decltype (http://en.cppreference.com/w/cpp/language/decltype).
Next would not work either:
void f() {
d();
}
void d() {
}

Assign value to private static variable in a class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
How to initialize private static members in C++?
(18 answers)
Closed 6 years ago.
I have a file A.hpp as such:
class A
{
private:
static std::string s;
public:
void modify_string();
};
I am implementing this in a file A.cpp as such:
#include "A.hpp"
void A::modify_string()
{
s = "something"; // Error here.
}
My main class:
int main()
{
A a;
a.modify_string();
}
I understand static variables are shared by all the class instances. I also went through this SO post where it says how to access the static member. Public static member of class . Could you please let me know where my concept is missing at?
Edit:
I am getting this error:
error: undefined reference to A::s
When you define:
void modify_string() {
s = "something"; // Error here.
}
You are creating a new function, not defining the member function modify_string of the class A. You need to do:
void A::modify_string() {
To inform the compiler that you are defining the member function modify_string for class A.
You also need a ; after your class definition.
Finally, the variable s is static so it needs to be defined seperatly somewhere so the linker can find a reference to it. So add:
std::string A::s = "default";
This was clearly described in the link you provided for your question.
Here is a working example: http://ideone.com/iQ6Kux
You need to reserve storage for s in exactly one compilation unit.
Do that by writing
std::string A::s;
In exactly one source file.
Your definition void modify_string() {...} in A.cpp is not defining the member function of the class, it's defining a separate global function with the same name. You probably meant
void A::modify_string()
{
s = "something";
}

this->member VS member in C++ [duplicate]

This question already has answers here:
Is there any reason to use this->
(16 answers)
Closed 7 years ago.
what is the difference between this:
int MyClass::getId() {
return this->id;
}
and this:
int MyClass::getId() {
return id;
}
in C++?
The first example limits the scope where the name "id" is looked up to members of class MyClass.
the second example does not.
This makes a difference if e.g. MyClass has no member "id", or if the current method has a local variable or parameter with the same name.
It depends on the context. Examples:
template <typename> struct X { int a; };
int b;
template <typename T> struct Foo : X<T> {
int f() {
return this->a; // OK
// return a; // Error
}
int g() {
// return this->b; // Error
return b; // OK
}
};
Assuming your class has a member variable named id, there is no difference. Even if you have a global id variable, the member variable always takes precedence.
Your next question is:
So why does exist the this keyword?
There are some situations you absolutely need it.
One example is you want a function returning a pointer/reference(*) to the object itself:
return *this; //reference
return this; //pointer
Other is you want to pass it as an argument to a function
some_function(*this); //reference(&) or value, depends on some_function signature
some_function(this); //pointer
It can also be used in this->id, just for legibility, just for the next person reading the code immediately know id is a constituent of the class instead of a global, having no functional advantage.
If you use a debugger, the this may be also an inspectable variable by it.

How to use . and -> in program when referring the variable from a class [duplicate]

This question already has answers here:
Difference between “::” “.” and “->” in c++
(5 answers)
Closed 9 years ago.
I am new to programming in c++.Can you please explain (with example if possible) in which case . and -> have to use when accessing variable from a class
:: is the scope resolution operator, used for referencing static class members and namespace elements.
-> is the indirect reference operator, used for referencing members methods and fields on an instance pointer.
. is the direct reference operator, used for referencing member methods and fields on an instance.
if you have pointer use -> if you have object or reference use . e.g. Say I have:
class foo{
public:
int a;
}
int main()
{
foo f;
foo* f1 = new foo();
cout<<f.a<<"\n"<<f1->a<<"\n";
}
If you have a pointer type and you want to access a member object or member function you can use (*ptr).anything or ptr->anything. In most cases that's exactly the same (unless of course a specific -> operator is overloaded). If you have a non pointer type you should use ..
This is simple, these both operators are used by an instance to access members or methods.
. is used by instance or instance reference
-> is used by instance pointer
In Example:
class MyClass
{
public:
int a;
}
int main()
{
// Instance, Reference, and Pointer declaration
MyClass instance;
MyClass& instanceReference = instance;
MyClass* instancePointer = new MyClass();
// Usecases
int a;
a = instance.a;
a = instanceReference.a;
a = instancePointer->a;
}

C++ static variable and function errors [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does it mean to have an undefined reference to a static member?
I have a static class as follows:
.h file
class c1 {
public:
static int disconnect();
private:
static bool isConnected;
};
.cpp file
#include c1.h
int c1::disconnect()
{
c1::isConnected = false;
return 0;
}
However when I compile, there is an error
undefined reference to `c1::m_isConnected'
Please help!
You have to provide an actual object instance for the static class members. Add the following to your .cpp file:
bool c1::isConnected;
To initialize it to a specific value, add an initializer:
bool c1::isConnected = false;
(By the way, classes in C++ cannot be static. Classes are just types. Only class members can be static.)
isConnected is a (non-static) member variable and you can't use it without an instance that it's a part of. While static variables exist independently of any object, non-static member variables only exist as part of an instance of that class.
You need to either make isConnected static or accept an instance of c1 on which to set isConnected to false. You probably want the former.
What you have in header file is declaration. However, you also need a definition of the variable. The correct code will look like this:
class c1 {
public:
static int disconnect();
private:
static bool isConnected;
};
bool c1::isConnected = false;
int c1::disconnect()
{
isConnected = false;
return 0;
}