I tried to make MyWindowSplitter class and I made the other new class derived from CView class as runtime class in MySplitter class. But when I tried to compile that I got these errors in MyProjectView.h in GetDocument function:
Error 1
error C2143: syntax error : missing ';' before '*'
Error 2
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Where is the problem and How can I fix them?
//MySplitter.cpp
#include "StdAfx.h"
#include "MySplitter.h"
#include "SplitDemoSixView.h"
#include "TestView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CMySplitter::CMySplitter(void)
{
}
CMySplitter::~CMySplitter(void)
{
}
void CMySplitter::ChangeViewClass(CRuntimeClass* pNewView)
{
m_pDynamicViewClass = pNewView;
}
void CMySplitter::DeleteView(int row, int col)
{
CView* pView = (CView*)GetDlgItem(IdFromRowCol(row, col));
if(pView->IsKindOf(RUNTIME_CLASS(CSplitDemoSixView)))
{
ChangeViewClass(RUNTIME_CLASS(CSplitDemoSixView));
}
else
{
if(pView->IsKindOf(RUNTIME_CLASS(CTestView)))
{
ChangeViewClass(RUNTIME_CLASS(CTestView));
}
}
CSplitterWnd::DeleteView(row, col);
}
//TestView.cpp drived from CView
#include "stdafx.h"
#include "SplitDemoSix.h"
#include "TestView.h"
#include "SplitDemoSixDoc.h"
#include "SplitDemoSixView.h"
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
CTestView::CTestView()
{
}
CTestView::~CTestView()
{
}
BEGIN_MESSAGE_MAP(CTestView, CView)
END_MESSAGE_MAP()
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
#ifndef _WIN32_WCE
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif
#endif //_DEBUG
// CTestView message handlers
And the problem is here:
//MyProjectView.h
#pragma once
#include "resource.h"
#include "MySplitter.h"
class CSplitDemoSixView : public CFormView
{
protected: // create from serialization only
CSplitDemoSixView();
DECLARE_DYNCREATE(CSplitDemoSixView)
public:
enum{ IDD = IDD_SPLITDEMOSIX_FORM };
// Attributes
public:
//ERROR PERFORMS IN THIS FUNCTION:
CSplitDemoSixDoc* GetDocument();
// Operations
public:
// Overrides
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate();
...
You need a forward declaration for your CDocument-derived class:
// Attributes
public:
//ERROR PERFORMS IN THIS FUNCTION:
class CSplitDemoSixDoc;
CSplitDemoSixDoc* GetDocument();
Related
I'm trying to create a game using C++ and SDL (I'm still new and learning).
I've been stuck on this for some time, and I'm not sure how to solve this.
This is Game.h where I'm trying to create the actual game:
#pragma once
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "TextureManager.h"
#include "GameObject.h"
#include "Input.h"
struct Objects
{
GameObject player;
Input input;
};
class Game
{
public:
Game() {};
~Game() {};
void init(int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool inline running()
{
return _isRunning;
}
static SDL_Renderer* renderer;
static SDL_Event event;
private:
bool _isRunning = true;
SDL_Window* _window = nullptr;
SDL_Texture* texture = nullptr;
GameObjectsRef data = std::make_shared<Objects>();
};
And this is GameObjects.h where I'm doing things related to objects of the game:
#pragma once
#include <string>
#include "TextureManager.h"
class GameObject
{
public:
GameObject() {};
~GameObject() {};
void init();
void render();
void update();
int xpos = 0;
int ypos = 0;
int dx = 0;
int dy = 0;
private:
SDL_Texture* texture = nullptr;
//GameObjectsRef data;
};
I found some answers where it says there might be issues with multiple includes, but shouldnt #pragma once take care of that?
Error messages in the Game.h lines 12 and 13
Error C3646 'player': unknown override specifier
Error C4430 missing type specifier - int assumed. Note: C++ does not support
Error C3646 'input': unknown override specifier
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
i am new to c++ programming and now faceing this "simple" problem for a while. I am implementing a simple step of a Observer-Pattern with the classes: Observer and Observable.
#ifndef OBSERVER_H
#define OBSERVER_H
#include "observable.h"
class Observer{
public:
virtual ~Observer() = default;
virtual void update(Observable* obs ) = 0;
};
#endif // OBSERVER_H
the Observabel Class Looks like that:
#ifndef OBSERVABLE_H
#define OBSERVABLE_H
#include <vector>
#include "observer.h"
class Observable
{
public:
Observable();
virtual ~Observable() = default;
void attach(Observer &o);
void detach(Observer &o);
void notify();
private:
std::vector<Observer*> observerlist;
};
#endif // OBSERVABLE_H
c++- file
#include "observable.h"
#include <algorithm>
void Observable::attach(Observer &o) { observerlist.push_back(&o); }
void Observable::detach(Observer &o)
{
observerlist.erase(std::remove(observerlist.begin(), observerlist.end(), &o));
}
void Observable::notify()
{
for (Observer* obs : observerlist) {
obs->update(this); // Here the IDE Shows the Error
}
}
Error:
C:\U...\observable.cpp:16: error: C2660: "Observer::update": function does not take 1 Argument
I really hope one of you can help.
greetings
I have structure: PrintChooseDlg.h
#ifndef PRINTCHOOSEDLG_H
#define PRINTCHOOSEDLG_H
#include <string>
#pragma once
#endif
class CPrintChooseDlg : public CDialog
{
public:
int choosing;
/*afx_msg void OnPrinter1();
afx_msg void OnPrinter2();*/
CPrintChooseDlg(CWnd* pParent = NULL);
enum { IDD = IDD_PRINTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
//afx_msg void OnPrinter1();
//afx_msg void OnPrinter2();
virtual void OnPrinter1();
virtual void OnPrinter2();
DECLARE_MESSAGE_MAP()
};
and PrintChooseDlg.cpp
// PrintChoose.cpp : implementation file
//
#include "stdafx.h"
#include "Tungsten.h"
#include "PrintChooseDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// PrintChoose
//IMPLEMENT_DYNAMIC(PrintChoose, CWnd)
CPrintChooseDlg::CPrintChooseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPrintChooseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChooseLabelDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPrintChooseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChooseLabelDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPrintChooseDlg, CDialog)
ON_BN_CLICKED(IDC_PRINTER1,OnPrinter1)
ON_BN_CLICKED(IDC_PRINTER2,OnPrinter2)
END_MESSAGE_MAP()
// PrintChoose message handlers
void CPrintChooseDlg::OnPrinter1()
{
choosing=0;
CDialog::OnPrinter1();
}
void CPrintChooseDlg::OnPrinter2()
{
choosing=1;
CDialog::OnPrinter2();
}
and in the main file where i am running from, i define the following headers:
#include "stdafx.h"
#include "Tungsten.h"
#include "TungstenDlg.h"
using namespace std;
#include<sstream>
#include <string>
The problem is i am always getting the following errors: error C2039: 'OnPrinter1': is not a member of 'CDialog'
error C2039: 'OnPrinter2': is not a member of 'CDialog'
What i tried is to add #include <string> at my header and make sure that the headers are not repeated, and defining Printer1 and Pronter 2 in the main file where i am running from, but i still get the same error. I appreciate your help. Thanks in Advance
To get the address of a member function, you need to use &CPrintChooseDlg::OnPrinter1.
Really old VC++, like VC6, didn't care about the correct syntax and generated incorrect message maps.
So I've got a bit of a problem (well two but they're unrelated to each other).
I have two headers that look as follows:
Game.h
#ifndef INIT_GAME_H
#define INIT_GAME_H
#include <deps/deps.h>
#include <handlers/RenderHandler.h>
class Game {
private:
RenderHandler* renderHandler; /* <-- This is line 32 in my actual header */
public:
Game() {};
~Game() {};
int initialise();
void handleEvents();
void update();
void render();
void clean();
}; // class Game
#endif // INIT_GAME_H
RenderHandler.h
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
#include <init/Game.h>
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
But the above gives me an error during compilation:
game.h(32): error C2143: syntax error: missing ';' before '*'
game.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(32): error C2238: unexpected token(s) preceding ';'
As you've probably guessed, I'm trying to store Game's instance in RenderHandler and vice versa. I'm probably doing it completely the wrong way but I can't figure why it's not working.
Also, all ; are in their right places prior to line 32 in my header file.
EDIT:
after doing the suggested forward declaration, I get the following error (now in RenderHandler.cpp file).
Error: pointer to incomplete class type is not allowed
This is what my code file looks like
RenderHandler.cpp
#include <handlers/RenderHandler.cpp>
void RenderHandler::initialise(Game* game) {
this->game = game;
}
void RenderHandler::render() {
glfwSwapBuffers(game->getPrimaryWindow());
}
Use forward declaration :
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
// FW declaration of Game
class Game;
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
I' m creating a simple MFC text editor in VS2010 in order to learn C++ and I've hit a bit of a stumbling block....
Specifically, I get the following error in my build log:
1> Generating Code...
1>Link:
1> Creating library C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.lib and object C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.exp
1>EmergenceDoc.obj : error LNK2019: unresolved external symbol "public: __thiscall CEmergenceCntrItem::CEmergenceCntrItem(struct _reobject *,class CEmergenceDoc *)" (??0CEmergenceCntrItem##QAE#PAU_reobject##PAVCEmergenceDoc###Z) referenced in function "public: virtual class CRichEditCntrItem * __thiscall CEmergenceDoc::CreateClientItem(struct _reobject *)const " (?CreateClientItem#CEmergenceDoc##UBEPAVCRichEditCntrItem##PAU_reobject###Z)
1>C:\Users\Alvin\Documents\Visual Studio 2010\Projects\Emergence\Debug\EmergenceHandlers.dll : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.52
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Below are all the files in which CEmergenceCntrItem appears....
cntritem.h:
#include "afxrich.h"
#include "Resource.h"
class CEmergenceDoc;
class CEmergenceView;
class CEmergenceCntrItem : public CRichEditCntrItem
{
DECLARE_SERIAL(CEmergenceCntrItem)
public:
CEmergenceCntrItem(REOBJECT* preo = NULL, CEmergenceDoc* pContainer = NULL);
public:
CEmergenceDoc* GetDocument()
{ return (CEmergenceDoc*)COleClientItem::GetDocument(); }
CEmergenceView* GetActiveView()
{ return (CEmergenceView*)COleClientItem::GetActiveView(); }
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWordPadCntrItem)
public:
protected:
//}}AFX_VIRTUAL
// Implementation
public:
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
EmergenceView.cpp:
#include "afxrich.h"
#pragma once
class CEmergenceCntrItem;
class CEmergenceView : public CRichEditView
{
protected: // create from serialization only
CEmergenceView();
DECLARE_DYNCREATE(CEmergenceView)
public:
CEmergenceDoc* GetDocument() const;
public:
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
public:
virtual ~CEmergenceView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
protected:
afx_msg void OnFilePrintPreview();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMutateGroup();
};
#ifndef _DEBUG // debug version in EmergenceView.cpp
inline CEmergenceDoc* CEmergenceView::GetDocument() const
{ return reinterpret_cast<CEmergenceDoc*>(m_pDocument); }
#endif
EmergenceDoc.cpp
#include "stdafx.h"
#include "Emergence.h"
#include "EmergenceDoc.h"
#include "CntrItem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CEmergenceDoc, CRichEditDoc)
BEGIN_MESSAGE_MAP(CEmergenceDoc, CRichEditDoc)
//{{AFX_MSG_MAP(CMyWordDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Enable default OLE container implementation
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS,
CRichEditDoc::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST,
ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()
CEmergenceDoc::CEmergenceDoc()
{
}
CEmergenceDoc::~CEmergenceDoc()
{
}
BOOL CEmergenceDoc::OnNewDocument()
{
if (!CRichEditDoc::OnNewDocument())
return FALSE;
return TRUE;
}
CRichEditCntrItem* CEmergenceDoc::CreateClientItem(REOBJECT* preo) const
{
return new CEmergenceCntrItem(preo, (CEmergenceDoc*) this);
}
void CEmergenceDoc::Serialize(CArchive& ar)
{
CRichEditDoc::Serialize(ar);
}
CntrItem.cpp
#include "stdafx.h"
#include "Emergence.h"
#include "EmergenceDoc.h"
#include "EmergenceView.h"
#include "cntritem.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_SERIAL(CEmergenceCntrItem, CRichEditCntrItem, 0)
CEmergenceCntrItem::CEmergenceCntrItem(REOBJECT *preo, CEmergenceDoc* pContainer)
: CRichEditCntrItem(preo, pContainer)
{
}
#ifdef _DEBUG
void CEmergenceCntrItem::AssertValid() const
{
CRichEditCntrItem::AssertValid();
}
void CEmergenceCntrItem::Dump(CDumpContext& dc) const
{
CRichEditCntrItem::Dump(dc);
}
#endif
Any help would be greatly appreciated.
Just copy cntritem.cpp and cntritem.h to resources folder!