Followers 0

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

## Create an account

Register a new account