vector inside a class

L

Lovens Weche

I'm trying to compile this code with VC++ 7:

class KeyboardInterface

{

public:

.....



std::vector<KBBits4KeyUp> KBKUBuffer(256, KBBuffer);

....



}

I get this error: error C2059: erreur de syntaxe : 'constant'

I guess we can't use the constructor of a vector in a class declaration,
right??? How can I solve this problem? thanx in advance...



Lovens
 
R

red floyd

Lovens said:
I'm trying to compile this code with VC++ 7:

class KeyboardInterface

{

public:

....



std::vector<KBBits4KeyUp> KBKUBuffer(256, KBBuffer);

...



}

I get this error: error C2059: erreur de syntaxe : 'constant'

I guess we can't use the constructor of a vector in a class declaration,
right??? How can I solve this problem? thanx in advance...

class KeyboardInterface
{
public:
//...
std::vector<KBBits4KeyUp> KBKUBuffer;
KeyboardInterface();
}

KeyboardInterface::KeyboardInterface() : KBKUBuffer(256, KBBuffer)
{
}
 
P

Peter_Julian

| I'm trying to compile this code with VC++ 7:
|
| class KeyboardInterface
|
| {
| public:
| ....
| std::vector<KBBits4KeyUp> KBKUBuffer(256, KBBuffer);
| ...
| }
|
| I get this error: error C2059: erreur de syntaxe : 'constant'
|
| I guess we can't use the constructor of a vector in a class
declaration,
| right??? How can I solve this problem? thanx in advance...
|

A class declaration requires a semicolon.

class KeyboardInterface
{
};

Lets consider a std::vector<int> and lets initialize a vector of 64
elements to 0 (hint: ctor initialization list)
---
// KeyboardInterface.h
#ifndef KEYBOARDINTERFACE_H_
#define KEYBOARDINTERFACE_H_

#include <vector>

class KeyboardInterface
{
std::vector<int> vn;
public:
KeyboardInterface();
~KeyboardInterface();
int getIndex(const int& n) const;
int getSize() const;
};

#endif // include guard KEYBOARDINTERFACE_H_
___

// KeyboardInterface.cpp
#include "KeyboardInterface.h"

const int NBUFFER(64); // could have been a member

KeyboardInterface::KeyboardInterface() : vn(NBUFFER, 0)
{
}

KeyboardInterface::~KeyboardInterface()
{
}

int KeyboardInterface::getIndex(const int& n) const
{
// error checking required: n must be less than NBUFFER
return vn[n];
}

int KeyboardInterface::getSize() const
{
return vn.size();
}
---
// test.cpp
#include "KeyboardInterface.h"
#include <iostream>

int main()
{
KeyboardInterface ki;
std::cout << "vn[0] = " << ki.getIndex(0) << std::endl;
std::cout << "vn[63] = " << ki.getIndex(63) << std::endl;
std::cout << "vector size = " << ki.getSize() << std::endl;

return 0;
}
---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top