C++: struct automatically compiled in namespace - c++

I'm using the d3dx9.h header to be able to use many of the Direct3D functions and datatypes. One of which is 'D3DXVECTOR3'. I have stumbled upon a weird issue of which I can't find the cause:
I have two files, Link.h and Link.cpp. Link is a static class. In my Link.cpp and Link.h I have included the d3dx9.h file as follows:
#ifndef D3DX9_INCLUDED
#define D3DX9_INCLUDED
#include <d3dx9.h> // Direct3D 9
#endif
In Link.cpp I have created a function to get a D3DXVECTOR3 variable:
#pragma once
#ifndef D3DX9_INCLUDED
#define D3DX9_INCLUDED
#include <d3dx9.h> // Direct3D 9
#endif
namespace Core
{
static class Link
{
private:
public:
static struct D3DXVECTOR3 getVector()
};
}
My Link.h script makes this public:
static struct D3DXVECTOR3 getVector();
Now, when I call this function from outside this class it keeps giving me the error
error C2027: use of undefined type 'Core::D3DXVECTOR3'
This is how I call the getVector function:
Core::Link::getVector();
I get the error even though D3DXVECTOR3 is not a member of 'Core'. I have included d3dx9.h the same way outside this class.
I have tried exactly the same with standard datatypes such as a bool, worked like a charm. Why does getVector return a D3DXVECTOR3 as a child of Core?

Related

C++ Create Instance of 2nd Class within Instance of 1st Class and Pass 1st Class as Paremeter

I'm using wxWidgets with CodeBlocks, and I'm trying to instantiate a class called FileManager as an attribute of the Frame for wxWidgets, and pass the wxFrame parent to the constructor of FileManager. The objective is to be able to able to refer to the wxFrame from within FileManager. I'm getting an error "FileManager does not name a type". Not sure what I'm doing wrong thanks
The project is called "MullSimple_CB" so the main frame class is "Mull_Simple_CBFrame" Here are the files.
So the main object is a wxFrame object of class MullSimple_CBFrame, defined inside MullSimple_CBMain. The class I want instantiated as a member of that class is FileManager
MullSimple_CBMain.h
#ifndef MULLSIMPLE_CBMAIN_H
#define MULLSIMPLE_CBMAIN_H
#include "non-wx/file_manager.h"
class MullSimple_CBFrame: public wxFrame
{
public:
MullSimple_CBFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~MullSimple_CBFrame();
// ERROR ON THIS LINE:
// "'FileManager' does not name a type"
FileManager fileManager(MullSimple_CBFrame parentFrame);
private
DECLARE_EVENT_TABLE()
};
#endif // MULLSIMPLE_CBMAIN_H
MullSimple_CBMain.cpp
#include "wx_pch.h"
#include "MullSimple_CBMain.h"
#include <wx/msgdlg.h>
#include "non-wx/file_manager.h"
MullSimple_CBFrame::MullSimple_CBFrame(wxWindow* parent,wxWindowID id)
{
FileManager fileManager(this);
}
FileManager.h
#ifndef FILE_MANAGER_H_INCLUDED
#define FILE_MANAGER_H_INCLUDED
#include "../MullSimple_CBMain.h"
class FileManager
{
private:
MullSimple_CBFrame storeMainFrame;
public:
// constructor
FileManager(MullSimple_CBFrame mainFrame);
};
#endif // FILE_MANAGER_H_INCLUDED
FileManager.cpp
#include "file_manager.h"
#include "../MullSimple_CBMain.h"
FileManager::FileManager(MullSimple_CBFrame mainFrame): storeMainFrame(mainFrame))
{
}
Your file_manager.h file includes MullSimple_CBMain.h and the MullSimple_CBMain.h file includes your file_manager.h.
You end up with a never ending chain of includes... which never resolve.
Consider putting forward declarations into a single .h file and then have your .h files only include it as opposed to including the individual class.h files themselves. The only time you need to include the class.h files themselves is if you need the compiler to know about the full definition of the class as opposed to just knowing the class exists at all.

C++ 'multiple definition' compile error with static enum class members

This is an Arduino project compiled in Visual Studio (using visual micro plugin). I am getting the following error:
AutonomyHandler.cpp.o (symbol from plugin): In function AutonomyHandler::setup() const
(.text+0x0): multiple definition of Module::AvailableCommandKeys
ArduinoProject.cpp.o (symbol from plugin)*: (.text+0x0): first defined here
I am using an enum of CmdKeys within the class definition, and I can use the line of code below to get the available set of keys, but when I try to use it, I get multiple compile errors as seen above for each place I have used it.
Module::AvailableCommandKeys
My Module.h looks as follows:
#ifndef _MODULE_h
#define _MODULE_h
class Module {
public:
enum CmdKeys { Forward, Left, Back, Right, Stop };
static const CmdKeys AvailableCommandKeys[2];
// other definitions...
};
const Module::CmdKeys Module::AvailableCommandKeys[] = { Forward, Back };
#endif
Does anyone know what is happening? I have had this issue before and making the members non-static fixed the issue but I want to keep these enum arrays static.
Since writing this post I found the answer so I thought I would post to help others anyway.
To fix the issue, you just need to move the initialisation of the static members from the definition file (.h) to the declaration file (.cpp)
Module.h looks as follows:
#ifndef _MODULE_h
#define _MODULE_h
class Module {
public:
enum CmdKeys { Forward, Left, Back, Right, Stop };
static const CmdKeys AvailableCommandKeys[2];
// other definitions...
}
const Module::CmdKeys Module::AvailableCommandKeys[] = { Forward, Back };
#endif
Module.cpp looks as follows:
#include "Module.h"
const Module::CmdKeys Module::AvailableCommandKeys[] = { Forward, Back };
// Other code...
Place the line:
const Module::CmdKeys Module::AvailableCommandKeys[] = { Forward, Back };
in a .cpp file.

How to use inheritance and avoid LNK2019 error

What is the correct way to use inheritance among multiple files?
I am new to C++, and I am trying to create a class for all my GDI+ related functions which I'm gonna use in my separate cpp files. I have tried several approaches and to be able to find the problem more easily I got to trying with empty constructor.
I get LNK2019 with this code (I took away parts which are unrelated to the issue, only what is related to WndFuncs class was left):
Header of functions file:
#ifndef WNDFUNCS_H
#define WNDFUNCS_H
class WndFuncs
{
private:
public:
WndFuncs(); //declaration
};
#endif
The file itself:
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <winuser.h>
#include <gdiplus.h>
#include "WndFuncs.h"
WndFuncs::WndFuncs() //definition
{
}
The header of class that tries to inherit the class:
#ifndef SEARCHEDITBOX_H
#define SEARCHEDITBOX_H
class SearchEditBox : public WndFuncs
{
private:
WndFuncs b;
SearchEditBox();
public:
~SearchEditBox();
static SearchEditBox* CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height, WndCols const* p_wndCols);
#endif
The the class file:
#include "stdafx.h"
#include "WndCols.h"
#include <windows.h>
#include "WndFuncs.h"
#include "SearchEditBox.h"
SearchEditBox::SearchEditBox()
: b()
{
}
SearchEditBox::~SearchEditBox()
{
if (editBox)
DestroyWindow(editBox);
}
SearchEditBox* SearchEditBox::CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height, WndCols const* p_wndCols)
{
SearchEditBox *p_SearchEditBox = new SearchEditBox; //allocating dynamic memory for class (which by itself is declared as pointer)
return p_SearchEditBox;
}
The error is:
LNK2019 unresolved external symbol "public: __thiscall
WndFuncs::WndFuncs(void)" (??0WndFuncs##QAE#XZ) referenced in function
"private: __thiscall SearchEditBox::SearchEditBox(void)"
(??0SearchEditBox##AAE#XZ)
I have read the explanation and all the points on the MSDN page (even tried putting "__cdecl" into the function declaration), I am sure the function is declared AND defined (in the class files; I also tried with const int x thinking that the problem may be in empty constructor), so the WndFuncs file should be fine.
I have read this and my assumption is that I declare the class in the wrong way in the inheriting file (and the linker thus can't link to correct functions in WndFuncs class), but even when I am trying to do everything as described in here it does not help either. I am not using any virtual members so the problem should not be related to that (as pointed out on that page).
When I add destructor to the WndFuncs class I get 2 LNK2019 errors, so the problem should not be related to that also. I also have the header files in right order I think (tried both).
I tried also with other function (with or without constructor) with same error.
The problem was solved by adding the class files to the project the correct way: Project > Add class. After that the references were linked correctly.
What I did wrong was adding files to the project separately (through: File > Add > File).

Getting errors when compiling game with GCC. (error: changes meaning of ”Screen” from ”class Screen” [-fpermissive])

Screen.h
#ifndef SCREEN_H
#define SCREEN_H
#include <SFML/Graphics.hpp>
class Screen
{
public:
virtual void handleInput(sf::RenderWindow& window) = 0;
virtual void update(sf::Time delta) = 0;
virtual void render(sf::RenderWindow& window) = 0;
};
#endif
Game.h
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <memory>
#include "Screen.h"
namespace sfSnake
{
class Game
{
public:
Game();
void run();
void handleInput();
void update(sf::Time delta);
void render();
static const int Width = 640;
static const int Height = 480;
static std::shared_ptr<Screen> Screen;
private:
sf::RenderWindow window_;
sf::Music bgMusic_;
static const sf::Time TimePerFrame;
};
}
#endif
I have problems with these two headers. The code compiles fine with visual studio but won't with GCC.
I get the errors:
Description Resource Path Location Type
error: changes meaning of ”Screen” from ”class Screen” [-fpermissive] Screen.h /Snake line 6 C/C++ Problem
error: declaration of ”std::shared_ptr<Screen> sfSnake::Game::Screen” [-fpermissive] Game.h /Snake line 28 C/C++ Problem
I have looked around for a while now and haven't found a solution. I really feel lost...
Also this is not my code it was written by the user 'jh1997sa' on reddit. The source on github. His thread on reddit.
You haven't named your platform, but I presume it's some flavor of Linux running X11. If so, this is most likely a name conflict with the Screen struct defined in X11/Xlib.h. SFML is almost certainly using Xlib behind the scenes to interact with the windowng system.
Because Xlib is a C library, all symbols it defines live in the global namespace. Fortunately in C++ you have the option of putting your Screen class in a namespace of your choosing. As long as you then refer to it by its fully qualified name, you can avoid the name clash.
From what I understand, if you are trying to compile on a linux system you should be using g++ to compile instead of gcc.

Awesomium c++: syntax error : missing ';' before '*'

I'm trying to integrate c++ code with awesomium functionalities, but I get many errors.
It seems that VisualStudio doesn't like my definition/declaration of the WebCore element. I copied it from http://wiki.awesomium.com/tutorials/tutorial-1-hello-awesomium.html.
I have simplified the code until this, and I still get the errors.
SimpleClass.cpp:
#include <Awesomium/WebCore.h>
include "SimpleClass.h"
using namespace Awesomium;
CSimpleClass::CSimpleClass(){
WebCore *web_core = WebCore::Initialize(WebConfig());
}
CSimpleClass::~CSimpleClass(){
}
SimpleClass.h:
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
WebCore *web_core;
};
Thanks!
Change your SimpleClass.h header to read:
#pragma once
#ifndef SIMPLECLASS_H
#define SIMPLECLASS_H
// forward declarations
namespace Awesomium{
class WebCore;
}
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core;
};
#endif /* SIMPLECLASS_H */
That way you announce to your compiler that there exists a type WebCore in the namespace Awesonium, and then you can use it to declare the member pointer CSimpleClass::web_core.
Potential dependency issues aside, the problem is that your header does not know that you want to use the Awesomium namespace.
Either (preferred) be explicit in the header about your definition of *web_core by doing
class CSimpleClass
{
public:
CSimpleClass(void);
~CSimpleClass(void);
Awesomium::WebCore *web_core; //note the use of Awesomium::
};
or (if you really must) include your header after your using directive
#include <Awesomium/WebCore.h>
using namespace Awesomium;
#include "SimpleClass.h"