Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have encountered one very strange problem. I am trying to explain full scenario here. Please suggest some solution.
/* "test.h" */
class A : public B
{
public:
A();
bool isUp;
};
/* test.cpp */
#include "test.h"
A::A()
{
isUp = false; //`isUp' was not declared in this scope
}
What is it means if I am declaring it in .h inside class. If I am wrong then what approach I need to follow.
EDIT :
class B
{
public:
sem_t m_job_count; //added by RajaGopal
B();
void Init();
void Init(char * s,int );
void RegisterWorker(worker *aWorker);
unsigned long getIndex();
void setIndex(unsigned long index);
char Msg[200];
static void* ThreadProc(void *p);
~B();
};
Where is the definition of class B. Since you are inheriting class B, compiler needs to know the definition of class B. Include its header file or its definition too.
Class definition of B should be visible to A.
Otherwise, I have compiled this code here and did not faced any problem.
Related
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 7 years ago.
Improve this question
Is a good practice to allways separate the declaration and the definition even if the definition has a only line, like the constructor or int value() const; in the code bellow?
My goal is to learn C++ and, at the same time, incorporate the best practices. So if there is something to improve in this code please tell me.
//counter.h
#ifndef COUNTER_H
#define COUNTER_H
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter();
int value() const;
public slots:
void setValue(int value);
signals:
void valueChange(int newValue);
private:
int m_value;
};
#endif // COUNTER_H
-
//counter.cpp
#include "counter.h"
Counter::Counter()
{
m_value = 0;
}
int Counter::value() const
{
return m_value;
}
void Counter::setValue(int value)
{
if(value != m_value)
{
m_value = value;
emit valueChange(value);
}
}
All functions implemented inside class definition is inline. So if you put these methods inside class definition you'll make them all inline. This is not equivalent code.
Best practice in C++ is moving to .cpp file as many definitions as possible and keeping your .h file as easy as possible. Reduce the number if #include directives in your header files. Use forward declarations of your classes and structs instead of full definitions where possible.
Alas, these guidelines will not work with templates, that's why C++ compilation is so slow.
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
#include<iostream>
#include<string>
using namespace std;
#ifndef TicTac_H
#define TicTac_H
class TicTac
{
public:
TicTac(int ,int);
void setpos(int);
void getpos(int);
void setpos2(int);
void getpos2(int);
bool takepos();
void setar(int&, int&);
void setarr();
void all(int,int);
void print();
int test();
private:
int p1;
int p2;
string tic[3][3] ;
string x;
string o;
int t1;
int t2;
bool ok;
};
#endif
**The compiler shown this message :
no default constructor exists for class "TicTac"
'TicTac' : no appropriate default constructor available
can anybody help me to fix this problem **
The error is surely not in that code, but in the code that includes that header and attempts to create an object of type TicTac without providing the two arguments that the constructor takes (two int). Other than that, the include guards should cover all the file (including the #include<...>) and you should never have a using directive (using namespace X) in a header.
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 9 years ago.
Improve this question
Take the following example:
// base.h
#include <string>
struct base
{
virtual ~base() = default;
virtual void do_something(const std::string& arg) const = 0;
};
// derived.h
struct derived : base
{
void do_something(const std::string& arg) const
{
//...
}
};
In this example should derived.h include the string header?
I fully agree with the principle of including what you use but in this case base.h has to include string and if the interface changes to not use string (and the include <string> is accordingly removed from base.h) then the interface will break highlighting anyways.
If the interface changes not to include string, your derived do_something will have to change as well. If you need in the derived.h independently of the interface (e.g. due to details unrelated to interface but present in the implementation) - in this case yes, it's valid and probably preferable to include <string> there as well.
I take the approach of always includeing anything I need for that file. I then don't have to go around hunting through headers and untangling webs of dependencies when I'm missing a symbol somewhere. Include guards mean that this is cheap.
But it's completely up to you. If you want to omit it, to make derived.cpp shorter, and add it in at a later date if the need arises, that's fine too.
There's simply no "right" answer.
You can include "base.h". Don't think too much.The less code you write,the better your code is.
#include "base.h"
struct derived :public base
{
void do_something(const std::string& arg) const
{
//...
}
};
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
can we call static function from constructor of same class.
class a{
static void fun();
a() {fun();}
};
it is giving an error while linking code. I am using visual studio C++, 2010.
Yes you can - as long as you provide a function definition for the static function as well.
I don't really understand the question.
If you provide a function definition as said by Billz and Ogni42, it will work.
The following code does compile, and work:
#include <iostream>
class a {
public:
a() { fun(); }
private:
static void fun();
};
void a::fun() {
std::cout << "BOAP" << std::endl;
}
int main() {
a foo;
return 0;
}
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 5 years ago.
Improve this question
I have a class, defined in a .h like this
#ifndef JLLABOUR_H
#define JLLABOUR_H
class JLLabour{
public:
JLLabour(int, int);
double* recursivefft(double*,int);
void FFT(int*);
~JLLabour();
private:
int width;
int height;
};
#endif // JLLABOUR_H
and in my .cpp I have the definition of my recursive function, the problem is that when I call it again , during compilation it doesnt allow me to continue because the method has not been defined yet. I dont know how to solve this, please help.
#include <JLLabour.h>
double* JLLabour::recursivefft(double* x,int asize){
//operations and declartions...
//...
even = recursiveFFT(sum,m); //<-- the problem is here, in the recursion.
odd = recursiveFFT(diff,m);
// more operations....
return result;
}
}
FYI I am compiling under Linux, using Qt because Im developing a graphic app...
C++ is case sensitive. Your method is called recursivefft not recursiveFFT.