How to split code into multiple files [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to split my code into multiple files. At this moment I have somethink like this but every time I need to include libraries and headers into each of these file.
Is it better way to do this?
main.cpp
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include "modules/intro.cpp"
#include "modules/login.cpp"
using namespace std;
int main() {
introModule();
login();
system("pause");
}
intro.cpp
#include <iostream>
using namespace std;
void introModule() {
// content of intro file
}
login.cpp
#include <iostream>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include "menu.cpp"
using namespace std;
#define ENTER 13
#define BACKSPACE 8
char passInputCharacter;
char password[20];
const char *accessPassword = "123";
int passInputCharacterPosition = 0;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void login() {
// content of login file
}

You should not include cpp files, only header files. Header files basically declare the interfaces of the corresponding cpp files. Therefore, for each cpp file, create an additional header file that contains function declarations only:
intro.h:
void introModule();
login.h
void login();
Then include the required header files in the cpp files:
In main.cpp:
#include "modules/intro.h"
#include "modules/login.h"
In intro.cpp:
#include "intro.h"
In login.cpp:
#include "login.h"

Related

An undefined reference error for files in a directory in c++ [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 2 years ago.
I'm working on a project, and inside of the project I've separated my own classes and functions into a directory called base_lib. When compiling the project, I keep getting an undefined reference error for one of the classes in this library. I have been trying to find the answer on many different forums and ultimately just decided to ask it myself. The code:
stack.h
#ifndef __STACK_H
#define __STACK_H
namespace stacker
{
/*******
My code here
*******/
}
#endif
stack.cpp
#include "stack.h"
#include <iostream>
using namespace std;
template <typename var>
stacker::Stack<var>::Stack()
{
head = nullptr;
tail = nullptr;
}
fix.h
#ifndef __FIX_H
#define __FIX_H
#include <iostream>
#include <string>
namespace fixes
{
/*******
My code here
*******/
}
#endif
fix.cpp
#include "fix.h"
#include "base_lib/stack.h"
#include <iostream>
#include <string>
using namespace std;
using namespace stacker;
main.cpp
#include <iostream>
#include "fix.h"
#include "base_lib/functions.h"
using namespace fixes;
using namespace mine;
using namespace std;
The compiler error
C:\...:fix.cpp:(.text+0xa2): undefined reference to `stacker::Stack<char>::Stack()'
...
Does anyone have any tips or tricks?
You seem to have a template in a C++ file instead of a header file.
This means the template will not compile, thus the error you are getting.
Read here: Why can templates only be implemented in the header file?

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

undefined reference to `PerformChat(char*, char*, char*, char*, char*)

I want to use ChatScript externally in my program. In the documents it says:
Embedding Step #1 First, you will need to modify `common.h and compile the system. You need to add all the CS .cpp files to your build list.
Find the // #define NOMAIN 1 and uncomment it. This will allow you to compile your program as the main program and ChatScript merely as a collection of routines to accompany it.
But I am newbie in Linux and can’t understand how to add .cpp files to my build list? What is my build list? May someone explains what do should I do exactly?
I did copy all the .cpp and .h and other folders existed inside ChatScript/SRC directory beside my main.cpp in my project.
Then I tried to run this code:
#include<iostream>
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But I get this error message:
undefined reference to `PerformChat(char*, char*, char*, char*, char*)
Then I did include all the header files to my program and delete this line of code: void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
#include<iostream>
#include "common.h"
#include "common1.h"
#include "constructCode.h"
#include "cs_ev.h"
#include "csocket.h"
#include "dictionaryMore.h"
#include "dictionarySystem.h"
#include "english.h"
#include "evserver.h"
#include "factSystem.h"
#include "functionExecute.h"
#include "infer.h"
#include "jsmn.h"
#include "json.h"
#include "mainSystem.h"
#include "markSystem.h"
#include "mongodb.h"
#include "mprintf.h"
#include "multi.h"
#include "my_sql.h"
#include "os.h"
#include "outputSystem.h"
#include "patternSystem.h"
#include "postgres.h"
#include "privatesrc.h"
#include "scriptCompile.h"
#include "spellcheck.h"
#include "systemVariables.h"
#include "tagger.h"
#include "testing.h"
#include "textUtilities.h"
#include "tokenSystem.h"
#include "topicSystem.h"
#include "userCache.h"
#include "userSystem.h"
#include "variableSystem.h"
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But the new error says:
error: conflicting declaration of C function ‘int main()'
You would have to include all the chatscript SRC files in your project to get the function PerformChat to compile. But shortly ChatScript will release with library compilations as well.

file does not name a type error on compilation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to write to an output file using the following code.
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <stack>
#include <vector>
#include <fstream>
using namespace std;
ofstream file;
file.open ("output.txt");
file.close();
When i try to compile I get the error file does not name a type but i clearly state it is ofstream on the line right before it. I'm not sure what's missing here. I've looked on forums for how to output to a file and this is the code given.
file.open ("output.txt");
file.close();
These are statements which have to be inside a function. In C++ oonly declarations can go in the globals scope. Try something like
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <stack>
#include <vector>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open ("output.txt");
file.close();
}
Which puts your code into a main function, which is called on program start.

Separate Class File errors? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm new to C++ and I am trying to separate class files for a game I made, but when i do, VS generates a load of errors.
Cube.h:
#include "stdafx.h"
#ifndef CUBE_H
#define CUBE_H
struct PlayerCube
{
//code
};
#endif //CUBE_H
Cube.cpp:
#include "cube.h"
#include "stdafx.h"
using namespace std;
PlayerCube::PlayerCube(){}
void PlayerCube::cube_movement(){}
void PlayerCube::show_cube(){}
Main:
#include "cube.h"
#include "stdafx.h"
using namespace std;
int main ()
{
//code
}
any ideas would help! :)
EDIT:
Keats answer reduced my errors from 96 down to 3!
I now just have 3 C2679 errors stating that "binary >> : no operator found"
EDIT:
Found out my problems, just one more remains!
Everything builds fine, but when I run my program, it crashes, ".exe has stopped working"?
This is specific to Visual Studio (precompiled headers) :
Remove the inclusion of stdafx.h from cube.h
Always include stdafx.h first in cpp files
Your code becomes :
Cube.h:
#ifndef CUBE_H
#define CUBE_H
struct PlayerCube
{
//code
};
#endif //CUBE_H
Cube.cpp:
#include "stdafx.h"
#include "cube.h"
using namespace std;
PlayerCube::PlayerCube(){}
void PlayerCube::cube_movement(){}
void PlayerCube::show_cube(){}
Main:
#include "stdafx.h"
#include "cube.h"
using namespace std;
int main ()
{
//code
}
If you still have errors, please include them in your question.