Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am learning my first programming language C++ and I have issues with making a private function public. Can you guys help me to find the problem?
#include <iostream>
#include <string>
using namespace std;
class JadClass
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
JadClass jc;
jc.setName = "Jad Charara w\n";
cout << jc.getName();
system("pause");
return 0;
}
instead of
jc.setName = "Jad Charara w\n";
write
jc.setName("Jad Charara w\n");
First of all you have defined 2 functions in class JadClass with public access specifier,so please confirm access specifier of which function you want to change from private to public.
Second thing in main you are trying to call setName function.
jc.setName = "Jad Charara w\n";
The above function call should be in jc.setName("Jad Charara w\n"); format.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?
If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I've tried to do a reference to a member variable, and understood that it does not work. Are there any way to do this? If not, is there a way to not constantly write "(* this)." ?
#include <iostream>
#include <string>
class Test
{
private:
std::string str_m;
void doSomething()
{
std::string& str = (*this).str_m; // does not work
std::cout << str << '\n';
}
public:
Test(std::string str): str_m(str)
{
(*this).doSomething();
}
};
int main()
{
Test test{"aaa"};
return 0;
}
VS compiler gave me: error C3867: 'Test::doSomething': non-standard syntax; use '&' to create a pointer to member
I've tried to do a reference to a member variable, and understood that it does not work. Are there any way to do this?
Yes:
struct Test {
std::string str_m;
void doSomething()
{
std::string& str = str_m;
}
};
[If not], is there a way to not constantly write "(* this)." ?
It is not clear from where you got the idea that you would have to write (*this).member. If you want to use this, then please write this->member, but there is no need to do this within the scope of the class (there are rare exceptions) and commonly it is frowned upon in favor of simply writing member.
PS: The error you report is from different code, not the one you posted.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Sorry if my English won't be clear for you, I'm a Russian student.
I have a code of a class with function declarations:
Account.h
class Account {
public:
Account();
virtual ~Account() = 0;
void tedt(const std::string &);
Account(std::string);
}
Account.cpp:
#include "Account.h"
void Account::tedt(const std::string& a) <===== error here
{
return;
}
Account::Account()
{
Account(""); <==== some other error is here...
}
Account::Account(std::string input) <===== and here!!!
{
SetLogin(input);
SetProxy("");
}
I see this message:
error: out-of-line definition of 'tedt' does not match any declaration in 'Account'
end this
error: out-of-line definition of 'Account' does not match any declaration in 'Account' (about Account::Account(std::string input))
And I don't know to do. I'm using qt creator for coding if it is important
As pointed out, you need to put a semicolon after the closing bracket of your class definition.
Please also note that your constructor without arguments will not work as expected here. Instead, you create a new temporary object with the argument. This will not change your current object. Better use:
Account::Account() : Account("") {}
, which works as expected.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Im trying to create a simple thread and have it execute.
My function definition is:
void MyClass::myFunction()
{
//Do Work
}
I'm creating the thread and executing it:
std::thread t1(myFunction);
Upon compiling my code, i get the following error:
error C3867: function call missing argument list; use '&MyClass::myfunction' to create a pointer to member.
Since my function does not take any parameters, i'm assuming i'm declaring it wrongly where i'm creating my thread? Any help will be appreciated, Thanks!!
If your method is a non-static member : You need an instance of your object to call the member function on.
If your method is static member, do what the compiler suggest : simply pass the address of your function.
Example:
class A
{
public:
void foo() { cout << "foo"; }
static void bar() { cout << "bar"; }
};
int main() {
std::thread t1(&A::foo, A()); // non static member
t1.join();
std::thread t2(&A::bar); // static member (the synthax suggested by the compiler)
t2.join();
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It is possible somehow to a call a non-member function from a member function?
Something like this:
class A {
int a;
public:
bool (int b) {
smt;
lala = define_smt(smt);
if (lala < 5) {
return true;
}
else {return false}
}
}
int define_smt(smt){ ...}
Thnaks in advance
The function must be known at call time. That means you must declare it before you use it. You have basically two choices here: Either put the whole function before the class, or use a function prototype before the class. So either:
int define_smt(smt) { ... }
class A {
// ...
}
or:
int define_smt(smt);
class A {
// ...
}
int define_smt(smt) { ... }
Of course It is Possible. As long as your function is available in the current context. i.e. either a prototype or a defination should be available, before the point of calling.