• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Psychopathetica

New to C++ Classes

7 posts in this topic

I'm working with VS 2010's C++. Been a VB guru for far too long and trying to transfer my skills to the C++ world. Anyway, with that said, I'm very familiar with classes, but finding it difficult to implement a simple class on C++. So heres what I got. I got a simple Vector class in a header file:

//Vector.h
#ifndef VECTOR_TEST
#define VECTOR_TEST

class Vector
{
public:
	float x,y;
}
#endif VECTOR_TEST

And when I tried declaring it, and displaying the result in a Messagebox, I got a lot of error messages. Most of which has nothing to do with me making an error in my code:

//Main.cpp
#include "Vector.h"
#include <stdio.h>
#include <Windows.h>

int main(void)
{	
	Vector* v = new Vector[2];
	v[0].x = 5;
	char Text[255];
	sprintf(Text,"%f", v[0].x);
	MessageBox(NULL, Text, "typedef", MB_OK);
	return 0;
}

And these are the errors I received:

1>------ Build started: Project: Header Test, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Vector' followed by 'unsigned' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Vector' followed by 'int' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2347: '__w64' : can not be used with type '__w64 Vector'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2371: 'size_t' : redefinition; different basic types
1>          c:\c++ self tutorial\header test\header test\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

These errors seem to be all based on sourceannotations.h. I never really included this header at all and have no idea why its even being brought up. I only got 2 measly files. Main.cpp and Vector.h. Now if I don't use the Vector.h header and simply just do this:

#include <stdio.h>
#include <windows.h>

Integer main(void)
{	
	charText[255];
	int Number = 5;
	sprintf(Text,"%i", Number);
	MessageBox(NULL, Text, "Message", MB_OK);
	return 0;
}

Then it works just fine, like its nothing. Could anyone help me get a simple class going from the Vector header and tell me whats wrong? Thanks in advance.

0

Share this post


Link to post
Share on other sites

Remember to read the error messages -- often the first one already contains a hint, just as in your case:

(did you forget a ';'?)

On a side note, please do NOT do that:

// an array of vectors is a grid, not a vector, so let's call it "grid" -- good variable names are important!
Vector* grid = new Vector[2]; // NO! memory leak if you forget to delete[]

In general, if you're just beginning, you should avoid using "new" like fire -- it implies manual memory management and should only be used if you have specialized needs (and, as such, you're no longer a beginner). Before you even consider using "new", make sure you have read and understood the following: http://www.informit.com/articles/article.aspx?p=1944072

If you meant to use a fixed-size array of "Vector", use an std::array:

std::array<Vector, 2> grid;

http://en.cppreference.com/w/cpp/container/array

If you'd like to have a resizable array instead, there's an std::vector for this:

std::vector<Vector> grid(2);

http://en.cppreference.com/w/cpp/container/vector

To add an element (automatically resizing your grid), simply use the "emplace_back" member function (or, if you'd like to waste some CPU cycles and/or memory, there's always "push_back" doing unnecessary copies (or moves, if you're lucky) ;-]):

 

A simple example: http://ideone.com/sVw9CO

#include <vector>
#include <iostream>

class Vector
{
public:
  float x,y;
};

int main()
{    
  // uninitialized grid[0] and grid[1]
  std::vector<Vector> grid(2);
  
  // (5, 3) stored in grid[2]
  // element constructed directly in "grid"
  grid.emplace_back(Vector{5.f, 3.f}); 
  
  // output: 5,3
  std::cout << grid[2].x << "," << grid[2].y << '\n';
  return 0;
}

HTH! :-)

Edited by Matt-D
2

Share this post


Link to post
Share on other sites

A class declaration needs a semi-colon at the end of it's closing brace.

 

To the upvoters - I don't believe I deserve so much just for pointing out a simple syntax error...

 

I think they've all been bit by that "bug" pretty hard themselves. So you got rewarded :)

2

Share this post


Link to post
Share on other sites

Wow guys thank you so much! Yea those semicolons are brutal sometimes. Thanks for the heads up. I didn't know classes needed to end in a semicolon. Sometimes I can feel like such a noob.

 

The vector class was actually gonna be used on vertices, but I never knew an array of vectors is called a grid. Its kind of an unusual name to call it that. But once the polygons are drawn, it does seem to look like a grid in the end doesn't it.

0

Share this post


Link to post
Share on other sites

The last line in your vector.h file is also invalid.  You need to remove VECTOR_TEST, you can do it this way to commenting the text out:

 

#endif //VECTOR_TEST

Edited by marek-knows.com
0

Share this post


Link to post
Share on other sites
I suggest you use "#pragma once" instead of header guards (the ifdefs) to save typing and avoid header file loaded define collisions. AFAIK its not standard, but is widely implemented and you can rely on it being there.
1

Share this post


Link to post
Share on other sites

Wow guys thank you so much! Yea those semicolons are brutal sometimes. Thanks for the heads up. I didn't know classes needed to end in a semicolon. Sometimes I can feel like such a noob.

The reason for this is that class definitions can be on the same statement as a variable that uses the class type. For instance:

 

struct Example{int member1; int member2;} example, *example2 = &example;

 

It's a relic from C that's not so useful in C++, though it's not that useful in C either.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0