LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Declaring a Vector inside a C++ Class (https://www.linuxquestions.org/questions/programming-9/declaring-a-vector-inside-a-c-class-797195/)

Aquarius_Girl 03-23-2010 12:19 AM

Declaring a Vector inside a C++ Class
 
Is it not possible to declare a vector inside a C++ class ?

Have a look at the following code:
Code:

#include <stdio.h>
#include <iostream>
#include <malloc.h>  // malloc
#include <strings.h> // bzero
#include <string.h>  // memcpy
#include <vector>

using namespace std;

class packedMessage
{
        protected:
                vector <unsigned int> numberOfBitsToBeExtractedVec;
                vector <numberOfBitsToBeExtractedVec> :: iterator numberOfBitsToBeExtractedIter;
        public:
                packedMessage  ();
                ~packedMessage ();
};

int main () {return 0;}

It is giving me the following errors:
Code:

vk.cpp:13: error: invalid use of non-static data member ‘packedMessage::numberOfBitsToBeExtractedVec’
vk.cpp:14: error: from this location
vk.cpp:13: error: invalid use of non-static data member ‘packedMessage::numberOfBitsToBeExtractedVec’
vk.cpp:14: error: from this location
vk.cpp:14: error: ‘packedMessage::numberOfBitsToBeExtractedVec’ cannot appear in a constant-expression
vk.cpp:14: error: template argument 1 is invalid
vk.cpp:14: error: template argument 2 is invalid
vk.cpp:14: error: expected ‘;’ before ‘numberOfBitsToBeExtractedIter’

What's wrong here ?

graemef 03-23-2010 02:12 AM

A quick look at you code would suggest to me that your iterator is set up incorrectly. It should probably be
Code:

vector<unsigned int>::iterator nobIter;

Aquarius_Girl 03-23-2010 02:17 AM

Quote:

Originally Posted by graemef
A quick look at you code would suggest to me that your iterator is set up incorrectly. It should probably be
Code:

vector<unsigned int>::iterator nobIter;

I don't know how to thank you for pointing out this senseless mistake to me ! I wish one could click thanks button more than once ..


All times are GMT -5. The time now is 08:20 PM.