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.
Related
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I had a situation where I wanted to use a default paramenter for a reference in a VERY large legacy codebase for a fix.
static bool _defaultValue = false;
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal = _defaultValue )
{
// ... code
}
My issue is with using a static variable inside a namespace just dangling there by itself.
This code is going to be reviewed before being shipped but I'm unsure if it would be considered bad practice to have a dangling static variable like that.
Without the variable you can't have a default value for a reference. My options are very limited to make other changes to get the desired effect.
Would this be considered "hacky unprofessional coding"?
My suggestion would be:
Remove the global variable.
Don't use a default value for the reference argument.
Create a function overload that has only one argument.
Call the first function from the second function.
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal)
{
// ... code
}
bool SomeFunction(const SomeComplexObject& iObj)
{
bool dummy;
return SomeFunction(iObj, dummy);
}
Client code can call whichever function is appropriate in their context.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm quite new to C++, and since I want my code to be good, I have a small question, let's say I have a function
UI::GetColor(CClass Class)
{
if (Class.m_Something)
return 0;
return 1;
}
Is it better to pass the CClass as a pointer, so it's not being copied or not? I saw a lot of code using different styles of that and I'm kind of confused which one is better and why. Thanks for answers.
The function should be written as follows:
class UI {
...
int GetColor(const CClass &c) const {
if (c.m_Something) {
return 0;
}
return 1;
}
}
The reference avoids an unnecessary copy; the const-parameter states that the parameter will not be changed; the const-function declarator states that the function will not change the this-object (i.e. the UI-instance on which it is called).
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
These functions are both public members of a class. Private members of the class include *theCharArray and *theFloat.
This one works fine:
void theClass::getCharArray(char charArrayParam[]) const
{
strcpy(charArrayParam, this->theCharArray);
}
This one underlines "this" and VS express says "Error: Expression must be modifiable value"
void theClass::getFloat(float theFloatParam) const
{
theFloatParam = this->theFloat;
}
Please tell me what I'm doing wrong.
In theClass::getCharArray(char charArrayParam[]), charArrayParam is passed basically as a pointer to character array without any idea of the buffer size. This is kind of risky with the risk of overflowing the buffer. Netter interface would be:
theClass::getCharArray(char *charArrayParam, int charArraySize) const {
strncpy(charArrayParam, this->theCharArray, charArraySize - 1);
charArrayParam[charArraySize - 1] = 0;
}
And for the second one:
void theClass::getFloat(float *theFloatParam) const
{
*theFloatParam = this->theFloat;
}
otherwise, since theFloatParam being passed by value, changing that within the function has no effect on the caller.