Jump to content

  • Log In with Google      Sign In   
  • Create Account


New to C++ Classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 20 June 2013 - 12:29 PM

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.



Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7649

Like
9Likes
Like

Posted 20 June 2013 - 12:35 PM

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...


Edited by mhagain, 20 June 2013 - 03:39 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Matt-D   Crossbones+   -  Reputation: 1432

Like
2Likes
Like

Posted 20 June 2013 - 04:57 PM

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, 20 June 2013 - 04:58 PM.


#4 Alpha_ProgDes   Crossbones+   -  Reputation: 4688

Like
2Likes
Like

Posted 20 June 2013 - 04:59 PM

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 :)


Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#5 Psychopathetica   Members   -  Reputation: 186

Like
0Likes
Like

Posted 20 June 2013 - 06:41 PM

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.



#6 MarekKnows.com   Members   -  Reputation: 448

Like
0Likes
Like

Posted 21 June 2013 - 10:36 AM

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, 21 June 2013 - 10:37 AM.

---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief


#7 Waterlimon   Crossbones+   -  Reputation: 2442

Like
1Likes
Like

Posted 21 June 2013 - 03:29 PM

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.

o3o


#8 King Mir   Members   -  Reputation: 1936

Like
1Likes
Like

Posted 22 June 2013 - 09:41 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS