sent string into function in other files c++ - 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

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

Invalid type of argument using pointer to struct array

I have a problem with the next code, I get the error: invalid type argument of unary '*' (have 'int'). If I write int *content that allows the code to run, but I have to write int=content and change the code *((ptab->content)+pC1+17), I tried but I can't fix the error.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
struct box{
int content;
};
struct box *ptab;
int pC1=5;
int main (){
ptab=new struct box[64];
if (*((ptab->content)+pC1+17)==0) {
pC1=pC1+17;
}
cout<<pC1<<endl;
}
I have to pass from pointers to poninters to struct arrays, this code is an example, because the original code has 23000 lines.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *box;
int pC1=5;
int main (){
box=new int[64];
if (*(box+pC1+17)==0){
pC1=pC1+17;
}
cout<<pC1<<endl;
}
With *((ptab->content)+pC1+17), an easier way to say it is ptab[pC1+17].content [the latter compiles and produces 22 as output]. Did you mean that or ptab->content + pC1 + 17 [which produces 5]?

Undefined reference to header file function

I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.

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;
};