Problem with creation of instances of classes inside another class - c++

I'm trying to create a class from within a class but I'm now very confused about how can that be done. I've tried for hours and hava had no luck, and my beginner level in c++ has limited me also a bit.
I've been using this link for reference with no success :/:
Creating instance in an another class of an object
This is my code:
Big class Sensor.h:
#ifndef SENSOR_H
#define SENSOR_H
#include "Dummy_Sensor.h"
#include "Lovato_DMG610.h"
class Sensor{
public:
double last10Vals [10];
char id[2];
Lovato_DMG610 dataSource(2);
Sensor(unsigned char dataSize){
}
int getSensorData ()
{
return 0;
}
private:
unsigned char dataSize;
};
#endif
My smaller class Lovato_DMG610.h:
#ifndef _KERN_LOVATO_DMG610
#define _KERN_LOVATO_DMG610
#include <Arduino.h>
//using namespace std;
class Lovato_DMG610{
public:
double variable = 0;
Lovato_DMG610(uint8_t pinToConnect)
{
_pinToConnect=pinToConnect;
}
private:
uint8_t _pinToConnect;
};
#endif
I've been getting an error related to the first file:
Sensor.h:10: error: 'Lovato_DMG610' does not name a type
Lovato_DMG610 dataSource(2);
My question for you guys would be:
How could i include the Lovato class in order to avoid this error?
How should the Lovato parameters should be set from Sensor class?
(this question is related to an error I'm also getting "Sensor.h:10: error: expected identifier before numeric constant
Lovato_DMG610 dataSource(2);")
I was using the code: using namespace std; before all class declarations and I get it is not a good practice (https://www.quora.com/What-does-using-namespace-std-mean-in-C++). Nevertheless, I would like to ask you if you could advise me whether this should be used in the scope of any of these headers.
Main program will keep an array of Sensor objects in which i want to make queries to different sensors. The only include in my Header.h is Sensors.h.
Thank you!
Update: After correcting the initialization as proposed by vll the constructor is ok (question 2 is resolved) but the same 'Lovato_DMG610' does not name a type' in Sensor.h error appears every time.

Parameters should be set in the constructor:
Lovato_DMG610 dataSource;
Sensor(unsigned char dataSize) : dataSource(2)
{}

Related

Why I can't use my class without parenthesis?(in C++)

//this is main.cpp
#include <iostream>
#include "Cook.hpp"
using namespace std;
int main(void){
int l = Cook.get_life();
}
//this is cook.hpp
#ifndef HUNTER_H
#define HUNTER_H
class Cook
{
public:
int get_life(void);
private:
int life;
};
#endif
//this is cook.cpp
#include "Cook.hpp"
int Cook::get_life(void)
{
life=0;
return life;
}
They are all in same folder. And I get compile error when I run main.cpp. And Xcode recommended to use Cook().get_life() instead of Cook.get_life(). Can you explain why? I thought I should use Cook.get_life.
I use Xcode.
First you have to declare variable with type of you class (instance) and then you can use it, But classes has static functions too, that mean you can use function without declare instance of it first but in that you can't use member variable of class, Reading more about concept of classes and more ...
get_life is not a static function, you have to call it on an instance of your class Cook, and that's exactly what Cook() does. If you want to call get_life without an instance of Cook, you should declare your function this way :
static int get_life(void);
And then call it like that :
Cook::get_life();
The thing is you can't use class attributes from static member functions, so instead you need to instantiate your class Cook before calling your member function.
Cook c = Cook(); // Cook().get_life() works to, but you don't keep your newly created object
c.get_life();

'Cashier' was not declared in this scope

I have this piece of code
#ifndef STATION_H
#define STATION_H
#include <vector>
#include "Dispenser.h"
#include "Cashier.h"
//class Cashier;
class Station
{
private:
int price;
int dispenser_count;
int cashier_count;
std::vector<Dispenser> dispensers;
std::vector<Cashier> cashiers;
public:
Station(int, int, int);
int GetPrice() const { return price; }
Dispenser *LookForUnoccupiedDispenser(int &id);
Dispenser *GetDispenserByID(int id);
Cashier *LookForUnoccupiedCashier();
};
#endif // STATION_H
When I have the class Cashier line commented, the compiler fails with a 'Cashier' was not declared in this scope error even though Cashier.h was included. Uncommenting it makes it possible to compile but I'm concerned that it shouldn't be happening.
Cashier.h
#ifndef CASHIER_H
#define CASHIER_H
#include "Station.h"
class Station;
class Cashier
{
private:
bool busy;
Station *at_station;
public:
Cashier(Station *employer);
bool IsBusy() const { return busy; }
};
#endif // CASHIER_H
How is it possible? It's clearly declared in the header file and as far as I know, #include does nothing more than pasting the content of a file into another one.
Thank you for the answers in advance!
Your station.h includes cachier.h. This is an example of cyclic dependency. Given that you only have a pointer to Station in Cachier I'd suggest removing the dependency of station.h and forward declare it.
An #include is literally nothing more than verbatim copy and paste of that file into the current compilation unit. The guards protect you from the effect of an infinite include cycle, but due to the guards one of the #includes does nothing, i.e. does NOT suck in the declaration (nor definition) of the respective class. This results in the error you get. In station.h the compiler has never seen any mention of the Cachier type when it sees the Station type.

Getting error: expected constructor, destructor, or type conversion before ‘(’ token

I am trying to make functions repository. I have created four files:
Function.hpp, Function.cpp, FunctionsRepository.hpp, FunctionsRepository.cpp
I want to keep pointers to functions in vector of pointers.
//FunctionsRepository.hpp
#ifndef FUNCTIONSREPOSITORY_HPP
#define FUNCTIONSREPOSITORY_HPP
#include <vector>
using namespace std;
class FunctionsRepository {
private:
static vector<double *> pointerToFunctions;
public:
static void addFunction(double * wsk);
};
#endif
//FunctionRepository.cpp
#include "FunctionsRepository.hpp"
void FunctionsRepository::addFunction(double * wsk) {
pointerToFunctions.push_back(wsk);
}
//Functions.hpp
#ifndef FUNCTIONS_HPP
#define FUNCTOINS_HPP
#include "FunctionsRepository.hpp"
int constFunction(int numberOfVehicles);
void linearFunction();
void stepFunction();
#endif
//Funcctions.cpp
#include "Functions.hpp"
double constFunction(double numberOfVehicles){
return numberOfVehicles/2;
}
double (*funcConstant)(double) = constFunction;
//ERROR HERE
FunctionsRepository::addFunction(funcConstant);
I want to add new functions to program as easily as its possible and use it leater in other parts of program.
But I dont get it. Why i am getting this error. The addFunction() method is static, that means I can use it in other classes or parts of program. Vector is static to make sure that is the only one copy for whole program.
Use function wrapper. std::function can stores callable objects. So, your code will contain something like this:
class FunctionsRepository {
private:
// void() - function prototype
static std::vector<std::function<void()>> pointerToFunctions;
public:
static void addFunction(std::function<void()> wsk)
{
pointerToFunctions.push_back(wsk);
}
};
for more information consult official documentation: http://en.cppreference.com/w/cpp/utility/functional/function
I solved It. I received an error because I was calling the FunctionsRepository::addFunction(funcConstant); expression out of any scope. I just created new function to execute this command and thats all.

C++ Beginner : calling default vs custom constructor

Beginner here - but i was uncertain what exactly to search for this (presumably common) question.
I am working on a program where I have a given class (Dictionary). I am supposed to make a concrete class (Word) which implements Dictionary. I should mention that I am not to change anything in Dictionary.
After making a header file for Word, I define everything in word.cpp.
I am unsure if I am doing this correctly, but I make the constructor read from a given file, and store the information in a public member of Word.
(I understand that the vectors should be private, but I made it public to get to the root of this current issue)
dictionary.h
#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Dictionary
{
public:
Dictionary(istream&);
virtual int search(string keyword, size_t prefix_length)=0;
};
#endif /* __DICTIONARY_H__ */
word.h
#ifndef __WORD_H__
#define __WORD_H__
#include "dictionary.h"
class Word : public Dictionary{
public:
vector<string> dictionary_words;
vector<string> source_file_words;
Word(istream &file);
int search(string keyword, size_t prefix_length);
void permutation_search(string keyword, string& prefix, ofstream& fout, int& prefix_length);
};
#endif /* __WORD_H__*/
word.cpp
#include "word.h"
Word(istream& file) : Dictionary(istream& file)
{
string temp;
while (file >> temp)
{
getline(file,temp);
dictionary_words.push_back(temp);
}
}
In word.cpp, on the line "Word::Word(istream& file)", I get this error :' [Error] no matching function for call to 'Dictionary::Dictionary()'.
I've been told this is error is due to "Word's constructor invoking Dictionary's ", but I still don't quite grasp the idea well. I am not trying to use Dictionary's constructor, but Word's.
If anyone has an idea for a solution, I would also appreciate any terms related to what is causing this issue that I could look up - I wasn't even sure how to title the problem.
Your child class should invoke parent constructor, because parent object are constructed before child. So you should write something like:
Word::Word(isteam& file) : Dictionary(file)
{
...
}
Seems its better described here What are the rules for calling the superclass constructor?

Undeclared identifier error c++, What did I do wrong i have no clue?

I am getting this error when ever i run this project
6 error C2065: 'Engine_in':undeclared identifier
I really dont know what i have done wrong. Usually i can figure it out and know what i did wrong but the books i have dont go into depth on seperate file classes. I honestly do not know where the error is coming from. I have googled it but everyones problems are specific, so that is why i am resorting to asking you to solve my problems. I appologize in advance for me not knowing much.
I have this class 'Engine_debug.cpp'
//Engine Debugger
#include<iostream>
#include "Engine_debug.h"
#include "Engine_in.h"
using namespace std;
Engine_debug::Engine_debug()
{
Engine_in input;
}
Then i have this header 'engine_debug.h'
#ifndef Engine_debug_H
#define Engine_debug_H
class Engine_debug
{
public:
Engine_debug();
protected:
private:
}
#endif
I also have this class 'Engine_in.cpp'
//Engine input
#include<iostream>
#include<string>
#include "Engine_in.h"
using namespace std;
Engine_in::Engine_in()
{
}
string askYN(string question, int format)
{...working code}
And one more, the other header 'Engine_in.h'
#ifndef Engine_in_H
#define Engine_in_H
class Engine_in
{
public:
Engine_in();
std::string askYN(std::string question, int format = 0);
protected:
private:
};
#endif
If anyone knows what i did wrong and would like to explain to me, please do, thanks.
If it isn't a typo, you forgot to write class name while defining the member function.
string Engine_in::askYN(string question, int format)
// ^^^^^^^^^^ Missed during member function definition
Not sure if that causes the kind of error message the compiler is complaining about.
There is also a missing ; at the end of Engine_debug class definition. Credits Jesse.