I'm new to using vectors, and I'm trying to declare a vector with size 1:

 vector<string> activities(1);

I get an error with this code, can anyone explain why?

G:\ManageActivities\Activity.h|11|error: expected identifier before numeric constant|
G:\ManageActivities\Activity.h|11|error: expected ',' or '...' before numeric constant|

Try

int main(void)
{
    vector<string> activities;
    activities.push_back("a new activity");
    cout << activities[0] << endl;
    return 0;
}

You don't need to pre-size vectors in c++. They will grow as needed.

Can you show a complete code sample that causes that error? That line looks fine on its own.

I don't know if this helps, this whole program has turned into a fuster cluck : S - I'm on the verge of starting from scratch:

#ifndef ACTIVTIY_H
#define ACTIVITY_H
#include "Event.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

class Activity : public Event
{
    private:
        vector<string> activities(1);
        string newA;

    public:

        Activity(string act, string type, int id, int m, int d, int y): Event(type, id, m, d, y)
        {
            newA = act;
        }

        void newActivity(string a)
        {
            newA = a;
            activities.push_back(newA);
        }

        string getActivity()const
        {

                return activities[1];


        }

};
#endif

You can't call constructors in the class body. You have to do it in the initializer list of your class's constructor. Like so:

class Activity : public Event
{
  private:
    std::vector<std::string> activities;
    std::string newA;

  public:

    Activity(std::string act, std::string type, int id, int m, int d, int y):
      Event(type, id, m, d, y),
      activities(1),
      newA(act)
    {}

    // ...
};

PS: You need to qualify standard library classes with the std namespaces unless you have using statements, which you shouldn't use in header files anyway.

#include <vector>
#include <string>
#include <iostream>

struct A
{
    // see: http://www.stroustrup.com/C++11FAQ.html#uniform-init
    // see: http://www.stroustrup.com/C++11FAQ.html#member-init
    std::vector<std::string> activities {1} ; // vector with initial size 1 (contains one empty string)

    // see: http://www.stroustrup.com/C++11FAQ.html#init-list
    std::vector<std::string> animals { "panda", "lynx", "ibex" } ; // vector with 3 animals (strings)

    std::vector<std::string> colours ;

    // see: http://www.stroustrup.com/C++11FAQ.html#uniform-init
    A() : colours { "cyan", "magenta", "yellow", "black" } {} // colours initialised with 4 colours (strings)
};

template < typename SEQUENCE >
void print( const SEQUENCE& seq, const char* name = "", std::ostream& stm = std::cout )
{
    stm << name << ":  [ " ;

    // see: http://www.stroustrup.com/C++11FAQ.html#auto
    // see: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& v : seq ) stm << '"' << v << "\" " ;

    stm << "]\n" ;
}

int main()
{
    A a ;
    print( a.activities, "activities" ) ; // activities:  [ "" ]
    print( a.animals, "animals" ) ; // animals:  [ "panda" "lynx" "ibex" ]
    print( a.colours, "colours" ) ; // colours: [ "cyan" "magenta" "yellow" "black" ]
}

http://coliru.stacked-crooked.com/a/6e298710d3d520cf

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.