2014-11-07 2 views
-2

У меня возникла проблема с созданием приложения WIN32 GUI на C++ (я использую Code :: Blocks для автоматического генерации main.cpp). Вот заголовочный файл, который я сделал для класса, который рисует объекты на экране.Компилятор: «экземпляр объекта» не называет тип

#ifndef CANVAS_H 
#define CANVAS_H 
#include <windows.h> 

class Point{ 
private: 
    const double default_x = 0.0, default_y = 0.0; 
public: 
    double x, y; 
    Point(); 
    Point(const Point& rval); 
    Point(double x, double y); 
    bool operator==(const Point& rval); 
    Point& operator=(const Point& rval); 
    Point distanceAway(double x, double y); 
}; 

class GraphicObject{ 
private: 
    int size; 
    Point offset; 
    Point* points; 
public: 
    GraphicObject(); 
    GraphicObject(const GraphicObject& rval); 
    void paint(HWND& hwnd, HDC hdc); 
    void addPoint(const Point& p); 
    //void removePoint(const Point& p); 
}; 
#endif 

А вот реализация:

#include "Canvas.h" 
#include <windows.h> 

//Point Class Definitions 
//Default constructor 
Point::Point(): x(default_x), y(default_y){} 
//Copy constructor 
Point::Point(const Point& rval): x(rval.x), y(rval.y){} 
//Constructor 
Point::Point(double x, double y): x(x), y(y){} 
//Equality operator 
bool Point::operator==(const Point& rval){ return (x == rval.x && y == rval.y); } 
//Assignment operator 
Point& Point::operator=(const Point& rval){ 
x = rval.x; 
y = rval.y; 
return *this; 
} 
//Function to find point certain distance away from calling object 
Point Point::distanceAway(double x, double y){ return Point(this->x + x, this->y + y); } 

//GraphicObject Class Definitions 
//Default constructor (makes a triangle) 
GraphicObject::GraphicObject(){ 
points = new Point[(size = 0)]; 
offset = Point(50,50); 
} 
//Copy constructor 
GraphicObject::GraphicObject(const GraphicObject& rval){ 
delete[] points; 
points = new Point[(size = rval.size)]; 
for(int i=0; i<size; i++){ points[i] = rval.points[i]; } 
} 
//Function to paint to screen 
void GraphicObject::paint(HWND& hwnd, HDC hdc){ 
hdc = GetDC(hwnd); 
for(int i=0; i<(size - 1); i++){ 
    MoveToEx(hdc, points[i].x + offset.x, points[i].y + offset.y, NULL); 
    LineTo(hdc, points[i + 1].x + offset.x, points[i + 1].y + offset.y); 
} 
MoveToEx(hdc, points[size - 1].x + offset.x, points[size - 1].y + offset.y, NULL); 
LineTo(hdc, points[0].x + offset.x, points[0].y + offset.y); 

ReleaseDC(hwnd, hdc); 
} 
//Function to add points to shape 
void GraphicObject::addPoint(const Point& p){ 
Point* temp = new Point[size];//DYNAMIC MEMORY temp ALLOCATED 
for(int i=0; i<size; i++){ 
    temp[i] = points[i]; 
} 
delete[] points; 
points = new Point[++size]; 
for(int i=0; i<(size - 1); i++){ points[i] = temp[i]; } 
delete[] temp;//DYNAMIC MEMORY temp DELETED 
points[size - 1] = p; 
} 

В main.cpp компилятор обнаруживает ошибку в строке сразу после моего экземпляра объекта класса GraphicObject.

#if defined(UNICODE) && !defined(_UNICODE) 
#define _UNICODE 
#elif defined(_UNICODE) && !defined(UNICODE) 
#define UNICODE 
#endif 

#include <tchar.h> 
#include <windows.h> 
#include <iostream> 
#include <chrono> 
#include <thread> 
#include "Canvas.h" 

/* Declare Windows procedure */ 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 

/* Make the class name into a global variable */ 
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp"); 

int WINAPI WinMain (HINSTANCE hThisInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR lpszArgument, 
       int nCmdShow) 
{ 
HWND hwnd;    /* This is the handle for our window */ 
MSG messages;   /* Here messages to the application are saved */ 
WNDCLASSEX wincl;  /* Data structure for the windowclass */ 

/* The Window structure */ 
wincl.hInstance = hThisInstance; 
wincl.lpszClassName = szClassName; 
wincl.lpfnWndProc = WindowProcedure;  /* This function is called by windows */ 
wincl.style = CS_DBLCLKS;     /* Catch double-clicks */ 
wincl.cbSize = sizeof (WNDCLASSEX); 

/* Use default icon and mouse-pointer */ 
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); 
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); 
wincl.hCursor = LoadCursor (NULL, IDC_ARROW); 
wincl.lpszMenuName = NULL;     /* No menu */ 
wincl.cbClsExtra = 0;      /* No extra bytes after the window class */ 
wincl.cbWndExtra = 0;      /* structure or the window instance */ 
/* Use Windows's default colour as the background of the window */ 
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 

/* Register the window class, and if it fails quit the program */ 
if (!RegisterClassEx (&wincl)) 
    return 0; 

/* The class is registered, let's create the program*/ 
hwnd = CreateWindowEx (
     0,     /* Extended possibilites for variation */ 
     szClassName,   /* Classname */ 
     _T("Code::Blocks Template Windows App"),  /* Title Text */ 
     WS_OVERLAPPEDWINDOW, /* default window */ 
     CW_USEDEFAULT,  /* Windows decides the position */ 
     CW_USEDEFAULT,  /* where the window ends up on the screen */ 
     544,     /* The programs width */ 
     375,     /* and height in pixels */ 
     HWND_DESKTOP,  /* The window is a child-window to desktop */ 
     NULL,    /* No menu */ 
     hThisInstance,  /* Program Instance handler */ 
     NULL     /* No Window Creation data */ 
     ); 

/* Make the window visible on the screen */ 
ShowWindow (hwnd, nCmdShow); 

/* Run the message loop. It will run until GetMessage() returns 0 */ 
while (GetMessage (&messages, NULL, 0, 0)) 
{ 
    /* Translate virtual-key messages into character messages */ 
    TranslateMessage(&messages); 
    /* Send message to WindowProcedure */ 
    DispatchMessage(&messages); 
} 

/* The program return-value is 0 - The value that PostQuitMessage() gave */ 
return messages.wParam; 
} 

int x = 0, y = 10; 
GraphicObject g; 
g.addPoint(Point()); 

/* This function is called by the Windows function DispatchMessage() */ 

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
PAINTSTRUCT ps; 
HDC hdc; 
switch (message)     /* handle the messages */ 
{ 
    case WM_PAINT: 
     //x++; 
     //hdc = BeginPaint(hwnd, &ps); 
     //hdc = GetDC(hwnd); 
     //Rectangle(hdc, 0, 0, 544, 375); 
     //TextOut(hdc, x, y, "MOVING TEXT WOO!", strlen("MOVING TEXT WOO!")); 
     //ReleaseDC(hwnd, hdc); 
     //EndPaint(hwnd, &ps); 
     g.paint(hwnd, hdc); 
     std::cout << "Window repainted\n"; 
     Sleep(5); 
     break; 
    case WM_DESTROY: 
     PostQuitMessage (0);  /* send a WM_QUIT to the message queue */ 
     break; 
    default:      /* for messages that we don't deal with */ 
     return DefWindowProc (hwnd, message, wParam, lParam); 
     InvalidateRect(hwnd, NULL, true); 
     UpdateWindow(hwnd); 
} 
return 0; 
} 

В частности, эта линия, где у меня есть проблема:

g.addPoint(Point()); 
+2

Этот код является _huge_. Где твой тест? –

+0

Если вы правильно отделите свой код, вы увидите, где проблема кроется очень легко. –

ответ

1

Вы пытаетесь иметь заявление, которое не является декларацией в области видимости пространства имен. Это запрещено: заявления, отличные от деклараций, могут отображаться только в теле функции.

Для применения операции к объекту в области видимости пространства имен, не будучи в функции, то есть, во время инициализации, вы можете использовать конструктор вспомогательного объекта, например:

GraphicObject g; 
namespace { 
    struct g_init { 
     g_init() { g.addPoint(Point()); } 
    } init; 
} 

Если тип GraphicObject является копируемым/возможно, легче инициализировать объект, используя вызов функции: