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?

Try

std::vector<int> v1;

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

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

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;
}

I copied and pasted what you had.

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

#include<vector> in the header file

Oh, and get rid of
std::

in

std::vector<int> v1;

So its just

vector<int> v1;

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

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

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

#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

Okay, to use

vector<int> 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.

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

Okay, to use

vector<int> 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;'

What do you mean by that?

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<int> 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.

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.

commented: Agreed +6

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.