954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Vector Problems

When writing a program that uses separate compilation (I have a header file, a .cpp file that declares all the member functions of the class in thee .h file, and an application file). I used a constructor to pass the contents of a dynamic array to a vector, but it is not passing properly.
Here is the constructor: (everything is declared properly, the constructor is a member of the class NumberSet)

NumberSet::NumberSet(int array1[])
{
    for (int e=0; e < array_size_horizontal; e++)
    {
        v1.push_back(array1[e]);
    }
}


The constructor is supposed to be used during the main function, but I can't get it to call properly. I also cannot get the array to be passed properly, though theses problems may be one and the same. Here is the declaration of the main function:

int main()
{
    
    int next;
    int horcounter = 0;
    int nextcounter = 0;
    int *array1;
    NumberSet runonce;
    
    cout << "Please enter the quantity of integers you will enter: ";
    cin >> runonce.array_size_horizontal;
    
    array1 = new int[runonce.array_size_horizontal];
    
    cout << "Please enter " << runonce.array_size_horizontal << " integers, then press return: ";
    while (cin >> next)
    {
          nextcounter++;
          array1[horcounter++] = next;
          if (nextcounter > runonce.array_size_horizontal-1)
          {
            break;
          }
    }

    NumberSet constructorcall()//I can't figure out what goes here

    return 0;
}


Also, when I have everything in one file, everything else compiles properly, but when they are in their seperate files, I get an error from the header file (below):

//File Name: SSassg1.h

#ifndef SSASSG1_H
#define SSASSG1_H

class NumberSet
{
      public:
             void add (int A); //adds an element to the set if not already present
             void remove (int A); //removes an element from the set if present
             void clear(); //clears the set if there are elements present
             int size(); //returns the number of elements in the set
             void output(); //outputs all elements properly
             NumberSet(int array1[]); //transfers the data from an array to a vector
             NumberSet( ); //default constructor
             int array_size_horizontal;
      private:
              vector<int> v1;
              int vectorsize;
};

#endif //SSASSG1_H


I get an error that says: "ISO C++ forbids declaration of `vector' with no type" (line 18). Also in Line 18, it says "expected `;' before '<' token". I don't know why it won't take this. Any ideas?

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Try

std::vector<int> v1;
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Now I get:
"using-declaration for non-member at class scope" and still the error "expected `;' before '<' token"

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Yeah, well that tells use nothing. Please post the current code and error and warning
messages.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This should be it. It's 3 files. Using Bloodshed Dev C++ 4.9.9.2, error message is "ISO C++ forbids declaration of 'vector' with no type". When using std::vector<int> v1; , the error is "using-declaration for non-member at class scope".


File: SSassg1.cpp

#include <iostream>
#include <vector>
#include "SSassg1.h"
using namespace std;

NumberSet::NumberSet(int array1[])
{
    for (int e=0; e < array_size_horizontal; e++)
    {
        v1.push_back(array1[e]);
    }
}

NumberSet::NumberSet( )
{
     //Body intentionally left empty
}


File: SSassg1.h

#ifndef SSASSG1_H
#define SSASSG1_H

class NumberSet
{
      public:
             NumberSet(int array1[]); //transfers the data from an array to a vector
             NumberSet( ); //default constructor
             int array_size_horizontal;
      private:
              std::vector<int> v1;
              int vectorsize;
};


File: SumnerApp1.cpp

#include <iostream>
#include "SSassg1.h" //make sure renamed properly;

using namespace std;

int main()
{
    int next;
    int horcounter = 0;
    int nextcounter = 0;
    int *array1;
    NumberSet runonce;
    
    cout << "Please enter the quantity of integers you will enter: ";
    cin >> runonce.array_size_horizontal;
    
    array1 = new int[runonce.array_size_horizontal];
    
    cout << "Please enter " << runonce.array_size_horizontal << " integers, then press return: ";
    while (cin >> next)
    {
          nextcounter++;
          array1[horcounter++] = next;
          if (nextcounter > runonce.array_size_horizontal-1)
          {
            break;
          }
    }
return 0;
}
iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I copied and pasted what you had.

The only solution I came up after compiling with no errors is:

#include in the header file

Oh, and get rid of
std::

in

std::vector v1;

So its just

vector v1;

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

You just straight copied my code and added #include <vector> in the SSassg1.h file and it compiled properly?

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

scratch that. I just rebuilt it again after no errors, and vector v1; gave me an error.

So, I tried std::vector v1; again. The only error after that was asking me to put a
#endif at the end of the HEADER file

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 
#ifndef SSASSG1_H
#define SSASSG1_H

#include<vector>

class NumberSet
{
      public:
             NumberSet(int array1[]); //transfers the data from an array to a vector
             NumberSet( ); //default constructor
             int array_size_horizontal;
      private:
		  std::vector<int> v1;
              int vectorsize;
};
#endif
Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

Okay, to use

vector v1;

without the need of the preceding std::, you need to include the

using namespace std;

at the beginning of the header file, else std:: will be required.

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 

Now that error is gone but I get "[Linker error] undefined reference to `NumberSet::NumberSet()'" and undefined reference to all my other member functions

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Okay, to use vector v1;

without the need of the preceding std::, you need to include the

using namespace std;

at the beginning of the header file, else std:: will be required.

Well its O.K. if its not in a header file where we can assume things like the user will use things like 'using namespace std;'

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

What do you mean by that?

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
Well its O.K. if its not in a header file where we can assume things like the user will use things like 'using namespace std;'


Being the newbie that I am. I can't say that is true. I had "using namespace std;" in the implementation AND application file but NOT in the header file as I attempted to build the project without the std:: preceding vector v1;. I got an error.

I included the std::, the error resolved. I removed std:: and included "using namespace std;" and the error was resolved.

Saith
Junior Poster
118 posts since Dec 2010
Reputation Points: 86
Solved Threads: 24
 
What do you mean by that?

Header files shouldn't assume anything. They should be simple and plain...without any preconceptions...we can't assume that the programmer will state 'using namespace std'.

Please read Accelerated C++ or Ruminations on C++ or C++ Primer...All these books state, that header files should be unqualified.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Maybe it's a compiler issue, but including using namespace std; or std:: still gives me an error - the Linker error I got above

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

In the wise words of - Lippman/Koenig and Moo don't assume anything. Write your header files uncomplicated...don't assume an environment that may not exist.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Here is my header file. It looks uncomplicated to me.

//File Name: SSassg1.h

#ifndef SSASSG1_H
#define SSASSG1_H

#include <vector>

using namespace std;
class NumberSet
{
      public:
             NumberSet(int array1[]); //transfers the data from an array to a vector
             NumberSet( ); //default constructor
             int array_size_horizontal;
      private:
             vector<int> v1;
             int vectorsize;
};

#endif //SSASSG1_H

Removing using namespace std; just goes back to previous error - that of forbidding declaration of vector without a type.

iamthesgt
Junior Poster
107 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: