473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

New C++. Why the flowing code cannot be compiled?

//---------------------------------------------------------------------------
// Matrix.h
//---------------------------------------------------------------------------
#if !defined(Matrix_H)
* #define Matrix_H
* #include <windows.h>
* #include <GL/gl.h>
* #include <iostream>
* #include <vector>
using namespace std;



class Matrix
{
private:
* static const int NUM_ELEMENTS = 16;
* vector<GLfloat> elements(NUM_ELEMENTS);
public:
* Matrix( void );
* void loadMatrix( Matrix other );
* void matrixMultiply( Matrix other );
* GLfloat getElement( int index );
* void toString();
* ~Matrix();
};

#endif
-----------------------------------------------------------------------
Matrix::Matrix( void )
{
* for ( int i = 0; i < NUM_ELEMENTS; i++ )
* {
*** if ( i % 5 == 0 )
*** {
***** elements[i] = 1.0;
*** }
*** else
*** {
***** elements[i] = 0.0;
*** }
* }
}
const int Matrix::NUM_ELEMENTS;

-------------------------------------------------------------------------
Howver, if I change vector<GLfloat> elements(NUM_ELEMENTS) to vector<GLfloat> elements(), it allows me to compile.

Thanks

Kai



--


殘**紅半邊劍,愁海飄零一*燈(Kai)




Jul 22 '05 #1
2 1196
"Chaokai Ching" <ch****@cis.ohio-state.edu> wrote...
//---------------------------------------------------------------------------
// Matrix.h
//---------------------------------------------------------------------------
#if !defined(Matrix_H)
#define Matrix_H
#include <windows.h>
#include <GL/gl.h>
#include <iostream>
#include <vector>
using namespace std;

class Matrix
{
private:
static const int NUM_ELEMENTS = 16;
vector<GLfloat> elements(NUM_ELEMENTS);

You cannot initialise non-static members in the class defniniton. That's
what the constructors are for.

[...]
-------------------------------------------------------------------------
Howver, if I change vector<GLfloat> elements(NUM_ELEMENTS) to vector<GLfloat> elements(), it allows me to compile.

Of course it does. 'elements' becomes a function declaration.

V
Jul 22 '05 #2
Chaokai Ching wrote:
Howver, if I change vector<GLfloat> elements(NUM_ELEMENTS) to
vector<GLfloat> elements(), it allows me to compile.

The code fixed to compile:

//---------------------------------------------------------------------------
// Matrix.h
//---------------------------------------------------------------------------
#if !defined(Matrix_H)
#define Matrix_H
#include <windows.h>
#include <GL/gl.h>
#include <iostream>
#include <vector>
using namespace std;

class Matrix
{
private:
static const int NUM_ELEMENTS = 16;
vector<GLfloat> elements;
public:
Matrix( void );
void loadMatrix( Matrix other );
void matrixMultiply( Matrix other );
GLfloat getElement( int index );
void toString();
~Matrix();
};

#endif

Matrix::Matrix( void ): elements(NUM_ELEMENTS)
{
for ( int i = 0; i < NUM_ELEMENTS; i++ )
{
if ( i % 5 == 0 )
{
elements[i] = 1.0;
}
else
{
elements[i] = 0.0;
}
}
}

// Not needed
//const int Matrix::NUM_ELEMENTS;
int main() {}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fraggle | last post by:
I am not sure if this is possible, and i cannot get google to tell me, so i am trying here! I wish to display a list of addresses, probably about 20, and i want them to be in a grid formation....
3
by: gallery | last post by:
This has stumped me. The text is not flowing properly to the left of an image I have floated to the right in IE. Seems fine in Firefox. I'm on a WinXP ... ...
14
by: Eric Lindsay | last post by:
I've seen a page using display, and especially display table that did some neat things with boxes, but basically it only worked with Mozilla browsers. Fell over fairly badly with Opera and Safari...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
20
by: Don Wash | last post by:
Hi Everyone! I'm new to .NET but I'm a experienced VB programmer (since VB 4) and wanting to move to VB.NET from VB 6. But as I read about .NET, I came across that compiled VB.NET code is...
14
by: greentiger1 | last post by:
I'm moderately experienced in CSS. I am currently working on a new version of my site, built from the ground up, but the dynamic portion, loaded with a PHP include into the 'chest' DIV is being...
2
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am...
8
by: wrungel | last post by:
Exceptions thrown by C++ function which is called by a C-function which is called by a C++ function are not catched by outermost C++ function. Operating system: Linux Compiler: GNU GCC Version...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.