How to use an array within a class? [duplicate] - c++

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 7 years ago.
I'm currently designing some code on QT that is built across multiple source. I want to create an array in one source and be able to access it in another source.
Currently in my Header I have class
Array_Class : public QString
{
public:
static QString Data_Array [2];
};
I don't think I need a constructor as I'm going to "populate" the array before I read it.
currently in my source.cpp I have
Array_Class::Data_Array[0]= "foo";
Array_Class::Data_Array[1]= "bar";
however this gives me the error message undefined reference to "Array_Class::Data_Array". What am I missing? thanks

So far, you have only declared your array:
Array_Class : public QString
{
public:
static QString Data_Array [2]; // -> only a declaration!
};
In order to use it, you must now define it. To do this, you need to place somewhere in your .cpp:
QString Array_Class::Data_Array [2];

Related

Pointer to a logger class provide for all other classes? [duplicate]

This question already has answers here:
Error logging in c++
(3 answers)
Closed 7 years ago.
I have a Logger class in my C++ application. This class has public methods like writeDebug(std::string & str) , can write to a debug output file and it works for me very good. The object is being created on application start by the first main class and there it is stored as a Logger * myLogger; object.
Since I have other classes as well, I would like to provide this logging functionality to those classes as well.
Currently I oberhead the myLogger pointer to toher classes in their contructor, and all the other classes store this very same Logger pointer when being created --> all other classes has this Logger * myLogger pointer stored:
Other_XY_Class::Other_XY_Class(Logger * logger, ...)
: localLoggerPtr{logger} { //.. }
==> localLoggerPtr (in class Other_XY_Class) == myLogger (in main application class)
I can imagine that there is a more convenient / elegant way to handle that ?
Since you only have one instance, why not use a Singleton for that?
Something along this lines:
class foo {
private:
void bar();
static foo Instance;
public:
static void Bar() { Instance.bar(); }
}

How to initialize static fields in C++? [duplicate]

This question already has answers here:
Defining static members in C++
(6 answers)
Closed 7 years ago.
I need a class with static std::vector<int> simples (first N simple numbers). I created it in static method __init__, which is called before any instance of MyClass is created:
class MyClass
{
public:
MyClass()
{
/* I need to use MyClass::simples here */
printf("%d\n", (int)MyClass::simples.size());
/* But I get the error here :( */
}
static void __init__(int N)
{
simples.push_back(2);
/* ...
here I compute first N simple numbers and fill
MyClass::simples with them
*/
}
private:
static std::vector<int> simples;
};
int main()
{
MyClass::__init__(1000);
MyClass var;
return 0;
}
But when I tried to use this vector in construction, I get undefined reference to 'MyClass::simples' error. How to solve my problem?
When defining a static member in C++, you need to write it two times: first in class definition as you did:
static std::vector<int> simples;
And then outside (preferably in an external .cpp file):
std::vector<int> MyClass::simples;
If you know about C language, this can help you: static members in C++ are comparable from global variables in C: defined as a prototype in .h file included whenever you need it, and value initialized in one .c/.cpp file.
You have to define the static data member outside the class
std::vector<int> MyClass::simples;
Within the class definition it is only declared.

How do I set static variables in C++ classes? [duplicate]

This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
I have a class like this:
class example {
public:
static void setStaticVar() { example::var = 1; };
private:
static int var;
};
But it gives me linker errors and I have no idea why.
I want to store some data in the variable that is the same for every instance. That's why I want to use a static variable instead of an instance variable (with an instance variable I would store the same data in every single instance of the class which is a waste of memory).
How do I do this?
In the source file
int example::var = 0;
You need to initialize the variable once. In one .cpp, outside of any functions, you have to initialize the variable:
int example::var = 0;
You must initialize it out of the class definition.
Try this.
class example { ... };
// initialize it to avoid linker errors
int example::var = 1;

Static Collection of Instances C++? [duplicate]

This question already has answers here:
Static constructor in c++ and fatal error LNK1120: 1 unresolved externals
(4 answers)
Closed 8 years ago.
I have a class Phone, I want it, when created to add itself to a static collection of phones. So I have the collection:
static vector < class Phone* > instances;
And in the constructor I do this:
Phone::instances.push_back(this);
But the linker throws an unresolved external symbol, why is that? What am I doing wrong? I didn't find a similar question. Is it necessary to add the instance outside the constructor? Or do I have to have the collection on another class? Thank you very much.
You must declare static member outside your class.
In your header:
class Phone{
...
static vector < class Phone* > instances;
...
};
In your cpp you need to create the instance of it:
//on global or namespace scope
vector <Phone*> Phone::instances;
Also, just a side note, not a direct answer to your question, it would be better to have a vector of "std::shared_ptr"s, rather than raw pointers. But if you a vector of std::shared_ptr, you will not be able to add "this" into that vector, so you would have to add one more thing to your class which will be able to access "this" in your class wrapped in std::shared_ptr. Here is what you can do:
class Phone : public std::enable_shared_from_this<Phone>
{
static vector<std::shared_ptr<Phone>> instances;
}
//then somewhere in your code:
Phone::instances.push_back(shared_from_this());
And in your .cpp file:
vector<std::shared_ptr<Phone>> Phone::instances;

What does class `expr_t : public ast_container` mean? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does `class HelloWorld : public Gtk::Window` mean?
I'm new to C++, but looked at a few tutorials and read partially Accelerated C++ ... but have seen something like
class expr_t : public ast_container {
public:
virtual double eval() const = 0;
...
What does the 1st line mean? Its declaring a class named expr_t? Then whats ast_container part?
Then on line 3: virtual double eval() const = 0 what does it mean?
Seems like its declaring a virtual function called eval that returns a double, but then whats the const = 0 part?
It means that the parent class/super class/(whatever one calls it) of the expr_t class is the ast_container class. At first glance, it roughly means that everything ast_container can do, expr_t can do it as well, and something more too.