Destructor not finding my namespace method [closed] - c++

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.

Related

cross include files in vs code [closed]

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

Iterating through an array of objects C++ [closed]

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
I am trying to iterate through an array to objects to set different attributes of those objects. The attributes of my objects may change over time.
My code as a simplified example:
// MyClass.h
class MyClass
{
/* class definitions */
};
extern MyClass object;
// MyClass.cpp
#include MyClass.h
/* Constructors, Destructors, Functions */
// main.cpp
void reload_objects();
int main()
{
MyClass object[20];
reload_objects();
{
void reload_objects();
{
for (int i = 0; i < 20; i++){
object[i].setProperties(/*args*/);
}
}
I am getting an error error: No match for 'operator[]' (operand types are ‘MyClass’ and ‘int’). If I move the for loop into main it compiles and runs fine.
What is causing this error?
Would it be easier or in some way better to use std::vector<MyClass> object(20) in some way?
There are several mistakes in your given code snippet. You can correct them as i have shown below. I have added comments wherever i have made changes.
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
// MyClass.h
class MyClass
{
/* class definitions */
public:
MyClass() = default;
};
extern MyClass object[20]; //declare object as an array
#endif
MyClass.cpp
// MyClass.cpp
#include "MyClass.h"
/* Constructors, Destructors, Functions */
main.cpp
#include <iostream>
#include "MyClass.h"
MyClass object[20]; // define object here instead of inside main()
void reload_objects(); // reload_objects() doesnt take any argument by looking at its call and definiton
int main()
{
// MyClass object[20]; //don't define object here
reload_objects();
}
void reload_objects() //you had a semicolon here so i removed it
{
for (int i = 0; i < 20; i++){
object[i].setProperties(/*args*/);
;
}
}
Also don't forget to add the definition and declaration of your other member functions like setProperties in the above example for it to work. These are some of the changes that i made:
You should declare object as an array of size 20 inside MyClass.h.
Define object outside of main() inside main.cpp as i did in my example.
Declare the function reload_objects() with no arguments in main.cpp.

How to use vector <struct> of other class [closed]

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.

Understanding #includes c++

I'm trying to understand some c++ code I stumbled across on the internet. It was the flex demo from NVIDIA with the awesome new fluid simulation and so I peeked a bit into the code. In there, I've seen something I didn't understand: There was a class called Scene in scene.h. It had no includes, yet it used the CreateRandomConvex function from the helpers.h file. Both of them where only included in the main.cpp but it didn't use either of them.
So my question now is: How can the Scene class access the helpers function without including it? And how does the compiler know where he can get the CreateRandomConvex function from if the Scene class has no includes?
Code:
main:
#include "B.h"
#include "A.h"
int main()
{
}
A.h:(Scene)
#pragma once
class Scene
{
void Init()
{
CreateRandomConvex();
}
};
B.h:(helpers)
#pragma once
void CreateRandomConvex()
{
//calc some stuff
}
#include "A.h" literally means ”insert the contents of the file "A.h" here”.
So when compiling the main file, the compiler sees
void CreateRandomConvex()
{
//calc some stuff
}
class Scene
{
void Init()
{
CreateRandomConvex();
}
};
int main()
{
}
As a side note, having one header depend on another already having been included isn't a very good practice.
To include A.h in a CPP file you must include the file B.h beforehand in that CPP file.
one can argue this is bad practice. But it works.

File Causing Thousands of Errors (C++) [closed]

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.