Multiple Class Files C++ - c++

I am unsure about the use of separate files for classes. How do I make functions inside the classes? Where do I put it?
QuizMain.cpp:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
using namespace std;
class QuizMain
{
public:
QuizMain();
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
return 0;
}
How would I make a class and call it correctly?

This is an example:
QuizMain.cpp file:
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain()
{
// Hia stackoverflow
}
void QuizMain::my_new_function(std::string my_name){
std::cout << "Hi " + my_name +"!" << std::endl;
}
QuizMain.h file:
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain();
void my_new_function(std::string my_name);
private:
};
#endif // QUIZMAIN_H
Main file:
#include <iostream>
#include <string>
#include "QuizMain.h"
using namespace std;
int main()
{
QuizMain qm;
qm.my_new_function("foo");
return 0;
}
Anyway, there is no point from asking such a question here. You should probably find a good book/resource and learn how to write and use functions.

Normally you have a header file and cpp file. The header file is where you declare your functions and member variables. The cpp file is where you implement your functions.
quizmain.h
// QuizMain.h file
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>
class QuizMain
{
public:
QuizMain(int quizScore);
// declare public functions here
private:
int quizScore; // declare private member variables here.
};
#endif // QUIZMAIN_H
cpp file
// QuizMain.cpp file
#include "QuizMain.h"
#include <iostream>
#include <string>
using namespace std;
QuizMain::QuizMain(int quizScore)
{
this.quizScore = quizScore; // init a quiz score
}
main
Call and create a class object like this
QuizMain quiz(95);

It is easy to do.
If you use the IDE project, the IDE set you to use the any file. Like code::block IDE But if you do not use the IDE project it is a little different to use.
You should write the .h file and then,after all writing you should put .cpp file.
Also you can use interface class that works by poiter.
/// .h file and declaration
#ifndef ONE.H
#define ONE.H
class one {
public:
one();
~one();
};
#include "one.cpp"
#endif ONE.H
then:
/// .cpp file and implementation
one::one(){
std::cout<<"constructor one"<<std::endl;
}
one::~one(){
std::cout<<"destructor one"<<std::endl;
}
then :
#include <iostream>
#include "one.h"
int main()
{
one o;
}
output:
constructor one
destructor one
Process returned 0 (0x0) execution time : 0.010 s
Press ENTER to continue.

Related

Why does code in constructor not run when called from another file?

main.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
int main(){
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::shreeman()
{
cout<<"Hello Hello"<<endl;
}
Why does this code not output "Hello Hello"? I have created an object in the main.cpp file, yet nothing is printed in the console.
In the Shreeman class, shreeman() (lowercase s) is not a valid constructor, it is just a member method. It needs to be renamed to Shreeman() (uppercase S) to be a construcor (the compiler should have warned you about that). C++ is case-sensitive, and a constructor name needs to match the class name exactly, case and all.
main.cpp
#include "shreeman.h"
int main()
{
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
Shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::Shreeman()
{
cout << "Hello Hello" << endl;
}

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

C++, Linking a locally defined struct to external function

I'm being tasked with defining a struct within my main() function, but using it in other files. I have the code working if I define my struct inside my header file, but I cannot figure out how to define struct inside main() and still use it outside its given scope.
Example of what I have now:
3 files: main.cpp, header.h, and function.cpp
main.cpp
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
vector<myStruct> myVec;
myFunction(myVec);
return 0;
}
header.h
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>
using namespace std;
struct myStruct{
int typeInt;
string typeString;
double typeDouble;
};
void myFunction(vector<myStruct>&);
#endif // HEADER_H_INCLUDED
function.cpp
#include <iostream>
#include <vector>
#include "header.h"
using namespace std;
void myFunction(vector<myStruct>& myVec){
myVec.push_back(myStruct());
myVec[0].typeInt=5;
cout<<myVec[0].typeInt<<endl;
}
Right now, this works for what I need it to do. Unfortunately, I'm told I cannot define struct myStruct inside header.h but instead must have it within main() in main.cpp.
I've tried changing my code to the following (function.cpp unchanged):
main.cpp v2
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
struct myStruct{
int typeInt;
string typeString;
double typeDouble;
};
vector<myStruct> myVec;
myFunction(myVec);
return 0;
}
header.h v2
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>
using namespace std;
template <typename myStruct>
void myFunction(vector<myStruct>&);
#endif // HEADER_H_INCLUDED
Now I receive the error:
error: 'myStruct' was not declared in this scope on line 7 of function.cpp.
How can I use myStruct in function.cpp, while still defining myStruct in main() of main.cpp?
this way using template is correct, that problem occur in function.cpp right?
you try to define the function "myFunction(..)" in header.h,
and remove function.cpp, this program will work well.
if you must implement that fuction in function.cpp, you have to create a template class.

Sharing a defined global variable in a header amongst two cpp files

I am interested in being able to share a defined global variable across two cpp files. Is the following possible? I am interested in this to avoid having to initialize the global shared variable multiple times. I am having trouble being able to build this code. How would you recommend to declare/define myMap in this case?
MapHeader.h
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include <string>
#include <map>
using namespace std;
extern const map<int, string> myMap = {{100 , "one hundred"}, {200,"two hundred"}, {300,"three hundred"}};
#endif // _MAP_HEADER_
FirstFile.h
#ifndef _FIRST_FILE_
#define _FIRST_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction1();
#endif
FirstFile.cpp
#include "FirstFile.h"
void myFunction1(){
cout << "myFunction1()" << myMap[100] << endl;
}
SecondFile.h
#ifndef _SECOND_FILE_
#define _SECOND_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction2();
#endif
SecondFile.cpp
#include "SecondFile.h"
void myFunction2(){
cout << "myFunction2()" << myMap[200] << endl;
}
Main.cpp
#include "FirstFile.h"
#include "SecondFile.h"
int main()
{
myFunction1();
myFunction2();
return 0;
}
I am getting the error message:
error: passing 'const std::map<>' as 'this' argument of 'std:map<>' .....
In MapHeader.h, change your definition to extern map myMap; and then move your definition exactly as you had it in MapHeader.h into one of the .cpp's.

"Unresolved External Symbol" errors when creating object in C++

I'm a pretty seasoned programmer but I'm just now diving into C++ and it's... well... more difficult than PHP and Python. I keep having unresolved external errors when trying to create an object from some classes. It's broken up into multiple headers and files but here is a basic idea from one of my classes:
die.h:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die();
};
#endif
die.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
using namespace std;
int Die::throwDie()
{
return 0;
}
sixsidedie.h
#ifndef SIXSIDEDIE_H
#define SIXSIDEDIE_H
#include "Die.h"
using namespace std;
class SixSideDie : public Die
{
public:
SixSideDie();
int throwDie();
private:
int randNumber;
};
#endif
sixsidedie.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Die.h"
#include "SixSideDie.h"
using namespace std;
const int SIX_SIDE = 6;
int SixSideDie::throwDie()
{
srand((unsigned int)time(0));
SixSideDie::randNumber = rand() % SIX_SIDE + 1;
return SixSideDie::randNumber;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
#include "SixSideDie.h"
#include "TenSideDie.h"
#include "TwentySideDie.h"
using namespace std;
int main()
{
Die* myDice[3];
myDice[0] = new SixSideDie();
myDice[1] = new TenSideDie();
myDice[2] = new TwentySideDie();
myDice[0]->throwDie();
myDice[1]->throwDie();
myDice[2]->throwDie();
system("pause");
return 0;
}
It keeps telling me that each object I create directly above is an unresolved external symbol and I just don't know why. Any thoughts!?
You declared a constructor for Die but never defined it.
Also, you almost certainly want throwDie to be virtual if you intend to override its behavior in derived classes, and you should never use using namespace std; in a header file (and many people, including me, would argue that you shouldn't use it at file-scope at all).
You didn't define your constructor in your cpp files.
Its good practice to define the constructors of the classes. Check this out:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die() { }; // can you spot the difference here?
};
#endif