Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
My question is: How to do cross including(I'm not sure if it is called like this) class files in visual studio code?
I found some reference here but I want to do use it like stdafx.h in Visual Studio(declaring every class objects in one file and use that everywhere).
ex: I have
main.cpp
CMyClass1.h, CMyClass2.cpp
CMyClass2.h, CMyClass2.cpp
ClassDefs.h
I want to use ClassDefs.h to declare class objects like
// ClassDefs.h
#include "CMyClass1.cpp"
#include "CMyClass2.cpp"
CMyClass1 g_myCl1;
CMyClass2 g_myCl2;
and use ClassDefs.h in CMyClass1/2 like these:
// CMyClass1.cpp
#include "ClassDefs.h".
//...
int CMyClass1::someFunc1()
{
g_myCl2.someFunc2();
}
// CMyClass2.cpp
#include "ClassDefs.h".
//...
int CMyClass2::someFunc22()
{
int val = g_myCl1.someVal;
}
(sorry for my English)
EDIT:
Referenced this link:
Create C++ Class in Visual Studio Code
You can use the "extern" keyword to get something like what you want. This allows you to access globals created in files that you aren't including through "#include". So your files might look something like this:
ClassDefs.h
#pragma once
//Include required header files for the classes you want to create the globals from
#include "CMyClass1.h"
#include "CMyClass2.h"
//Using the extern keyword to give any file that includes ClassDefs.h access
//to globals created elsewhere.
extern CMyClass1 g_myCl1;
extern CMyClass2 g_myCl2;
CMyClass1.h
#pragma once
class CMyClass1 {
public:
void somefunc1();
int someVal;
};
CMyClass1.cpp
#include "ClassDefs.h"
CMyClass1 g_myCl1; //create the global here
void CMyClass1::somefunc1() { g_myCl2.someFunc(); }
CMyClass2.h
#pragma once
class CMyClass2
{
public:
void someFunc();
int someVal;
};
CMyClass2.cpp
#include "ClassDefs.h"
CMyClass2 g_myCl2; //create the global here
void CMyClass2::someFunc() { int val = g_myCl1.someVal; }
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
insertItem (value_type )
{
}
Most code functions i have used so far have been along the lines of
insertItem( value_type var) Which allows me to access the data through var. In these new functions the only thing inside the parathesis is a object type. I cant access an object by saying only its type.
The functions in the header files do not use any variable names so i cant add to the functions in my implementation
No, you're wrong in saying the above statement. It is optional to name the parameters in the declarations(in header files) of the member functions. If you choose to not name the parameters in the declarations in header files, you can still name the parameter in the implementation files. One such example to get you started is shown below:
header.h
#pragma once
#include <string>
class Name
{
public:
//setter. Note here you can skip naming the parameter
void setName(std::string);
//constructor
Name(std::string); //note that here also we can skip naming the parameter
private:
std::string name;
};
source.cpp
#include "header.h"
//note here in implementation we've named the member function parameter to be p
void Name::setName(std::string p)
{
name = p;
}
//here in implementation we've named the constructor parameter to be p
Name::Name(std::string p): name(p)
{
}
main.cpp
#include <iostream>
#include<string>
#include "header.h"
int main()
{
Name n1("anoop");//this uses constructor
n1.setName("rana");//this uses the setter setName
return 0;
The output of the above program can be seen here
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
main.cpp:
#include "headerone.hpp"
int main() {
menus::menu();
}
headerone.hpp:
#pragma once
#include "headertwo.hpp"
namespace menus {
void menu() {
object instance;
}
}
headertwo.hpp:
#pragma once
#include "headerone.hpp"
class object {
public:
object() {
method();
}
void method() {
int x; /* do something */
}
~object() {
menus::menu();
}
};
I get an error in the deconstructor saying that menu namespace cannot be found.
Error C2653 'menu': is not a class or namespace name
The menu() function should only be declared in the header file. The definition should be in its associated .cpp source file.
The headerone.hpp file should look like this:
#pragma once
namespace menus {
void menu();
} // menus
The headerone.cpp file should look like this:
#include "headerone.hpp"
#include "headertwo.hpp"
namespace menus {
void menu() {
object instance;
}
} // menus
One of the areas that many good C++ books omit are how source files, header files, and projects should be put together. There is one good — albeit rather long in the tooth (but still relevant) — book called Large-Scale C++ Software Design by John Lakos which discusses project layout and suggested implementation in great detail.
He also has a more recent book in two volumes called Large-Scale C++, which is not a replacement for the earlier book. Rather it has an even broader and more ambitious scope for software development. Volume I is Process and Architecture, and Volume II is Design and Implementation.
You have cyclic dependency in your program.
You can solve this by using source files(.cpp) in addition to header files(.hpp) as shown below:
headerone.hpp
#pragma once
#include "headertwo.hpp"
namespace menus {
void menu(); //this is a declaration
}
headerone.cpp
#include "headerone.hpp"
namespace menus
{
//this is a definition
void menu() {
object instance;
}
}
headertwo.hpp
#pragma once
//no need to include headerone.hpp here
class object {
public:
object() {
method();
}
void method() {
int x; /* do something */
}
~object();//this is a declaration
};
headertwo.cpp
#include "headertwo.hpp"
#include "headerone.hpp"
object::~object() {
menus::menu();
}
main.cpp
#include "headerone.hpp"
#include "headertwo.hpp"
int main() {
menus::menu();
}
The output of the above modified program can be seen here.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
//TreeBox.h
#pragma once
#include "ListBox.h"
struct ItemInfo
{
std::string _fileName;
std::string _childName;
std::string _time;
std::string _format;
std::string _size;
std::string _nodeKey;
};
class TreeBox
{
...
}
//ListBox.h
class
{
public:
std::vector<ItemInfo> _itemList; //compile error
}
I wanna use std::vector in "ListBox.h" from using struct in "TreeBox.h"
but it was compile error allocator C2903
how do I use that?
You need not to include ListBox.h in TreeBox.h
Always follow practice of including standard libraries first and then the .h files
#pragma once
#include <vector>
#include <string>
class ListBox;
struct ItemInfo
{
std::string _fileName;
std::string _childName;
std::string _time;
std::string _format;
std::string _size;
std::string _nodeKey;
};
class TreeBox
{
};
#pragma once
#include <initializer_list>
#include "TreeBox.h"
class ListBox
{
public:
std::vector <ItemInfo> _itemList;
private:
};
Simply add #include <vector> to the top of your header to include the STL vector. #including a file header at the top of a file allows you to access the objects defined in that header.
You're including ListBox.h at the beginning of TreeBox.h. Specifically, you're including it before the part of TreeBox.h that defines ItemInfo.
So, you're trying to define a vector<ItemInfo> before the compiler knows what an ItemInfo might be, and it doesn't like that.
Rearrange your definitions so by the time the compiler sees the code trying to create a vector<ItemInfo>, it has already seen the definition of ItemInfo (and be sure you've included <vector>, in case you don't have that yet).
So with things in the correct order, you'd have something like this:
#include <vector> // First, tell the compiler about `vector`
class ItemInfo { // ...and `ItemInfo`
// ...
};
class ListBox {
std::vector<ItemInfo> itemList; // *Then* we can use `vector` and `ItemInfo`
// ...
};
If you want some of the pieces of that to be in separate headers that you #include, that's fine--but you still need to get the definitions in the right order.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I can understand that headers are used to include classes and functions into another file, but .cpp just seem a bit useless to me.
.h files are used to declare functions that can be exported/imported, however, template functions and classes must be defined in the same .h file and not only declared.
I have only used .cpp files as of now for the main program, but can they be used for any other thing? I am using .h files for everything and just .cpp files for the int main() function
Question: What are the cases where you use .cpp files over .h files
Here's an example of how to organize your code. Define headers for any classes you're using, like:
// example.h
#pragma once // Avoid duplicate imports, if supported by your compiler
#include <string>
class Example {
public:
Example();
Example(const char* name);
~Example();
protected:
std::string name;
};
Then implement it:
#include "example.h"
Example::Example() {
}
Example::Example(const char* name_) : name(name_) {
}
Example::~Example() {
}
Then in main.cpp:
#include "example"
int main() {
Example e("Some Name");
return 0;
}
This will all change for the better when modules become the best way to declare things, but until then, this is how it's typically done.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This class below is causing a horrendous amount of errors. It appears to be fine though. Any C++ Gurus around who know why VC++ hates me?
Entity.h
#pragma once
#include "World.h"
#include "Renderer.h"
class Entity {
public:
Entity(World* world, Coordinate coord);
~Entity();
void render(Renderer renderer) const;
World* world;
Coordinate coord;
};
Entity.cpp
#include "Entity.h"
Entity::Entity(World* world, Coordinate coord) : world(world), coord(coord) {
world->entities.insert(this);
}
Entity::~Entity() {
world->entities.erase(this);
}
The errors themselves don't make a whole lot of sense as they aren't even related to this file. Some of the common errors are unexpected end of file, missing ';' before '{' and "Entity is not a class or namespace name". Those errors do not occur when I do not include Entity in my project. The last of those errors appear in the declaration code of Entity.
The errors (With all duplicates removed): http://pastebin.com/TEMEhVZV
World.h
#pragma once
#include <map>
#include <vector>
#include <unordered_set>
#include "Chunk.h"
#include "Coordinate.h"
#include "Renderer.h"
class World {
public:
~World();
void generateChunk(Coordinate coord);
void loadChunk(Coordinate coord);
void renderWorld(Renderer* renderer);
std::unordered_set<Entity*> entities;
inline Chunk* getChunk(Coordinate coord) const {
return loadedChunks.at(coord);
}
private:
std::map<Coordinate, Chunk*> loadedChunks;
};
Renderer.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include "World.h"
class Renderer {
public:
sf::Window *window;
void bind(sf::Window* newWindow);
void initializeOpenGL();
void renderChunk(Chunk* chunk);
inline void drawPoint(Coordinate coord) {
glBegin(GL_POINTS);
glVertex3d(coord.x, coord.y, coord.z);
glEnd();
}
private:
template <class T>
inline static void pushVector3(std::vector<T>* vertices, T x, T y, T z);
};
To me, it looks like a circular header dependency, meaning something can't be defined.
If your Renderer.h file has a method acting upon an Entity object, and contains this header file as a dependency, Entity will have to be declared before Renderer can be compiled. (The compiler needs to know how big an Entity object will be so it can hard-code the stack offset.)
But similarly, Renderer needs Entity. So it can't be compiled, either!
This may not have shown up in your project before, because the header files are loaded in a different order than now, when the 'Entity' header triggers them.
So, what you should do is modify the headers so there are no circular dependencies, and then reference only pointers in the header, since they have fixed, known sizes. Here are some tricks:
Include low-level classes instead of higher ones.
#include "World.h"
-->
#include "Coordinate.h"
class World;
Use pointers.
#include "Renderer.h"
void render(Renderer renderer) const;
-->
class Renderer;
void render(Renderer* renderer) const;
Doing these, the header files can be moved to your .cpp file:
#include "Entity.h"
#include "World.h"
#include "Renderer.h"
Try going to the very first error its spits out, and fixing that one. In VC++ double-clicking there should take you to the line in question. Often times after the first error or two the compiler is so hopelessly confused that nothing else in its output is worth looking at.
My suspicion would be that it will take you to a line in one of those header files you are not displaying.
It's hard to give too much help without more context. In my experience, errors like this almost always relate to a missing semicolon. Are you given a file and line number with those errors? I would check in Renderer.h, and make sure it is not missing a semicolon.
The reason I suggest this is because, when you #include a file, the compiler actually copies it in to this file. That means that typos in previous files can manifest themselves in these files. If you can post more information, or even copy and paste the errors themselves, I'll try to be more helpful.
EDIT:
Since you've posted your errors, this makes much more sense. If you look, the first error in the list is actually number 148. You have to scroll down for error number 1:
"Error 1 error C2065: 'Entity' : undeclared identifier world.h"
To me, this looks like you're trying to use the class Entity in the file world.h, and it doesn't exist yet. So this looks like a circular include problem.