C++ : Transfer the Program into Header - c++

Im new in C++, and i got a program called sendSMS:
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
void *func_servidor(void *ptr_timer);
ServerSocket server(30001);
pthread_t thread_servidor;
pthread_t thread_transfdados;
pthread_t thread_BD;
pthread_cond_t cv;
pthread_mutex_t mp;
int ret;
#define CTRL_C "\x1A"
const int PORT_MON = 30000;
const string serialPort = "/dev/ttyS0";
using namespace LibSerial;
using namespace std;
int setSerial(SerialStream& ssStream, const string& port) {
...
....
}
int sendsms(int argc, char **argv) {
bool send = true;
...
....
return(0);
}
void *func_servidor(void *ptr)
{
...
....
return(0);
}
I want to transfer all those functions to a header .h file and call it in a main program(main.cpp), so the main.cpp only calls "everything" like:
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main{
SendSms();
return(0);
}
And the header will be like?
#ifndef __SENDSMS__
#define __SENDSMS__
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "Socket.h"
#include <pthread.h>
#include <stdio.h>
#include "Config.h"
#include <mysql/mysql.h>
#include <cstdlib>
#include <SerialStream.h>
class sendsms
{
private:
int ret;
const int PORT_MON;
int argc;
const string serialPort;
char **argv;
bool send;
public:
sendsms();
void *func_servidor(void *ptr);
int setSerial(SerialStream& ssStream, const string& port);
int sendsms();
};
#endif

In your main.cpp you will need an object of type modem in order to call sendsms() from main():
#include sendSMS.h
class modem{
{
public:
void SendSms();
}
int main(int argc, char **argv) {
modem m;
m.SendSms();
return(0);
}

Related

Included Class Type is apparently not declared, why?

I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.

Referee.cpp:10:18: error: no matching function for call to ‘Human::Human()’ Referee::Referee()

I'm new to c++ and was having problem with doing some inheritance, does any one know why im getting this error? (its the only error I'm getting when compiling).
I compiled with g++ -o. Also I'm so sorry in advance if I'm doing a lot of things wrong here, I'm very new to c++. >_<
Please let me know how I can make my code better or more efficient.
computer.h
#ifndef RPS_H
#define RPS_H
#include <iostream>
#include <string>
#include <stdio.h>
class Computer
{
public:
Computer(std::string);
~Computer();
char charc;
};
#endif
human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <iostream>
#include <string>
#include <stdio.h>
class Human
{
public:
Human(std::string);
~Human();
char charh;
};
#endif
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
class Referee : public Human{
public:
Referee();
~Referee();
bool Winneris();
};
#endif
Computer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "computer.h"
using namespace std;
Computer::Computer(string char_c)
{
}
Computer::~Computer()
{
}
Human.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
using namespace std;
Human::Human(string char_h){
char_h=charh;
cout<<"r/p/s?"<<endl;
cin>>charh;
}
Human::~Human()
{
}
Referee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "referee.h"
using namespace std;
Referee::Referee(){
}
bool Referee::Winneris(){
if (charh=='r'){
cout<<"draw"<<endl;
}
else if(charh=='p'){
cout<<"Victory!"<<endl;
}
else if(charh=='s')
{
cout<<"Defeat"<<endl;
}
return 0;
}
Referee::~ReReferee(){
}
main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
#include "computer.h"
#include "referee.h"
using namespace std;
string char_h;
string char_c;
// main program
int main()
{
Human *round1h;
round1h = new Human(char_h);
Computer *round1c;
round1c = new Computer(char_c);
Referee *round1r;
round1r = new Referee();
round1r -> Winneris();
}
When you have written the parameterized constructor in respective classes. You have created the class objects, which call the default constructor which takes no parameter.
You have to define the default constructor as well in your respective classes.
Human::Human()
{}
Computer::Computer()
{}
Referee::Referee()
{}
Constructor types

sent string into function in other files c++

How I can sent string variable into function in other file?
main.cpp:
#include <iostream>
#include <conio.h>
#include <string>
#include "headers.hpp"
using namespace std;
int main()
{
string a;
cout<<"Type:"<<endl;
cin>>a;
other(a);
getch();
return( 0 );
}
headers.hpp:
#ifndef HEADERS_HPP
#define HEADERS_HPP
void other(string a);
#endif
function.cpp:
#include "headers.hpp"
#include <iostream>
#include <math.h>
using namespace std;
void other(string a){
cout<<a;}
I don't know why it doesn't work. Do you know solution?
If you are saying it does not compile, you have to move the #include <string> from main.cpp into headers.hpp

Enum ifstream and getline()

I've got the problem with following code. It should get the object parameters from the txt file. Of course to get enum values correctly I've made this simple getline() function. I'm keep getting errors which I've tried to repair, but I still have no idea what's wrong. VC says that 'no instance of overloaded function getline matches the argument list'.
My code:
CKomputer.cpp
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include "CKomputer.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"
#define TESTPR1
using namespace std;
void Komputer::pobierz(string nazwa)
{
#ifdef TESTPR1
cout<<"Uruchomiono metode pobrania stanu obiektu Komputer z pliku"<<endl;
#endif
ifstream plik_wejsciowy;
plik_wejsciowy.open(nazwa+".txt");
int zastos;
getline(plik_wejsciowy,linia);
zastos=atoi(linia.c_str());
zastosowanie=Komputer::zastosowanie(zastos);
plik_wejsciowy >> nazwa_komputera >> ram >> ile_kart_dzwiekowych >> zastos;
procesor.wczytaj(plik_wejsciowy);
if (ile_kart_dzwiekowych>0)
{
for (int i=0;i<ile_kart_dzwiekowych;i++)
karta_dzwiekowa[i].wczytaj(plik_wejsciowy);
}
plik_wejsciowy.close();
}
CKomputer.h
#pragma once
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include "UrzadzenieElektroniczne.h"
#include "Procesor.h"
#include "KartaDzwiekowa.h"
using namespace std;
enum zastosowanie{biznes, gaming, grafika, programowanie};
class Komputer: public UrzadzenieElektroniczne
{
private:
Procesor procesor;
KartaDzwiekowa *karta_dzwiekowa;
protected:
string nazwa_komputera;
int ram;
int ile_kart_dzwiekowych;
public:
Komputer();
Komputer(string nazwa_komputera2, int ram2, int ile_kart_dzwiekowych2);
Komputer(const Komputer& k);
Komputer(int ilosc_kart);
~Komputer();
static int ile_Komputerow;
static int licz_ile_Komputerow();
void komp_info();
void wlaczenieurz();
void zapisz(string nazwa);
void pobierz(string nazwa);
zastosowanie zastos;
};

pthread_create() "class not declared in the scope?

I have a class called pos... I am trying to poll a method from this class. I used pthread_create(pthread_t thread, pos::Pirnt_data,this);
I get an error that pos is not declared in the scope... I included the h file of pos but I don't understand. I think I am using a wrong format can somebody help me
#include "position.h"
#include "pthread.h"
#include "pos.h"
void position::tick(schedflags_t flags)
{
if(pthread_create(&thread,NULL,pos::Print_data,this)!=0) {
stringstream bad;
bad << "OPIMex: Could not create listener thread: "
}
this class position has method tick that runs every 1 second with the data. I am trying to poll a method Print data from the class pos but it gave me that error any ideas why ?
this is class pos.h
#ifndef POS_H_
#define POS_H_
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <gps.h>
#include <string.h>
#include <pthread.h>
#include <string>
#include <vector>
#include <strings.h>
#include <math.h>
using namespace std;
namespace herpderp {
namespace modules {
int UBX_step =0;
long data;
int UBX_class;
int UBX_id=0;
int UBX_payload_length_hi;
int UBX_payload_length_lo;
int UBX_payload_counter =0;
int ck_a;
int ck_b;
int GPS_timer;
int fd;
unsigned int UBX_buffer[35];
int payload_data;
long lat=0;
long lon=0;
long alt_MSL=0;
long iTOW=0;
long alt=0;
unsigned long LastMS;
int UBX_Read;
vector <float> v;
fstream myfile;
int Open_port(void);
int read_tofile();
long join_4_bytes( unsigned int Buffer[]);
void parse_ubx_gps(void);
void checksum(char ubx_data);
void Print_data();
int push_data_into_vector();
int decode_gps();
int Configure_gps();
int test();
int Close_NEMA();
int Open_UBX();
}
}
#endif //POS_H_
pthread_kill is not on pthread.. It is on signal.h
#include <signal.h>
1) you can provide some code snippet/additional information to help you better.
2) If you are getting linkage error, check if you have linked with -lpthread library.
Form the pos.h it seems that there is no class called pos and you just need to call the function name:
if(pthread_create(&thread,NULL,Print_data,this)!=0) {