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.
Related
This question already has answers here:
Code-runner configuration for running multiple cpp classes in vscode
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've been following tutorial on how to create class using header files and came to a problem even if I did everything like in tutorial. I got Cat.h, Cat.cpp and main.cpp files. All of them are in the same folder.
Cat.h:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void speak();
};
#endif
Cat.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::speak()
{
cout << "Meeeow!" << endl;
}
main.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
Cat jim;
jim.speak();
return 0;
}
When i run the program I got error: "undefined reference to `Cat::speak()'". The problem is solved when i add line #include "Cat.cpp" to main.cpp but I dont think thats a way to go and tutorial was done without that.
Solved. For anyone having the same problem using VS Code with Code Runner extension, I found the solution in different thread:
Code-runner configuration for running multiple cpp classes in vscode
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?
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/
This question already has answers here:
Why stdfax.h should be the first include on MFC applications? [duplicate]
(3 answers)
Closed 5 years ago.
I expect this is incredibly simple but can't for the life of me figure out whats wrong. I'm new to C++ and I've created a C++ class in visual studio and am trying to use it in the main method of another file. I've stripped everything back to the bare minimum but still I can't get it to run. The first compile error I get is 'Test': undeclared identifier. If I remove 'Test test;' from App.cpp it all compiles fine.
Below is the code. Can anybody help?
App.cpp:
#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
Test test;
//cout << test.getNumber() << endl;
return 0;
}
Test.h:
#pragma once
class Test
{
private:
int number;
public:
int getNumber();
Test();
~Test();
};
Test.cpp:
#include "stdafx.h"
#include "Test.h"
int Test::getNumber()
{
return number;
}
Test::Test()
{
this->number = 1;
}
Test::~Test()
{
}
The problem is that "stdafx.h" is pre-compiled.
Visual c++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The solution is to move it to be the first include.
You can read more about it in the wiki page of Precompiled header.
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"