Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:35:54+00:00 2026-05-31T16:35:54+00:00

I’m trying to simply use a vector within one of my classes. When trying

  • 0

I’m trying to simply use a vector within one of my classes. When trying to access the vector it tells me that it’s undefined (but I’ve defined it in my header).

I have two classes, Person and Dog. A person can own one or more dogs so I want to add each dog a person owns into an array. This should be real simple so this problem is really starting to get to me. Here’s some code:

The class Person.cpp:

#include "Person.h"
#include "stdafx.h"
#include <iostream>

using namespace std;

Person::Person(string name, string address, int age)
    :name(name),
    address(address),
    age(age)
    {}
int Person::getAge(){
    return age;
}
std::string Person::getDogInfo(int index){
}
void Person::addDog(string dogName, string breed){
    dogCollection.push_back(Dog(dogName, breed));
}
std::vector<Dog> getDogs(){
    return dogCollection;  //dogCollection undefined error here
}

And here’s Person.h:

#ifndef Person_H
#define Person_H
#include <vector>
#include "Dog.h"
using namespace std;
class Person{
    public:
        Person(string name, string address, int age);
        string getName(){return name};
        string getAddress(){return address};
        void addDog(string dogName, string breed);
        string getDogInfo(int index);
        std::vector<Dog> getDogs();
        int getAge();

    private:
        string name;
        string address;
        int age;
        std::vector<Dog> dogCollection;
};
#endif

If you want to have a look at my dog classes I’ll paste them as well:

Dog.cpp:

#include "stdafx.h"
#include <iostream>
#include "dog.h"

Dog::Dog(string dogName, string breed)
    :dogName(dogName),
        breed(breed){}

std::string Dog::Dog.getDogName(){
return dogName;
}

std::string Dog::Dog.getBreed(){
return breed;
}

and Dog.h:

#ifndef Dog_H
#define Dog_H
#include <iostream>
using namespace std;

class Dog{
public:
    Dog(std::string dogName, std::string breed);
    std::string getDogName();
    std::string getBreed();
private:
    std::string dogName;
    std::string breed;
};
#endif

Also, I just want to add that this is no homework. I’m used to java and I’m only trying to learn some C++ since I need it for future work.

EDIT: Updated the code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:35:55+00:00Added an answer on May 31, 2026 at 4:35 pm

    This is incorrect (and unrequired):

    dogCollection = new std::vector<Dog>; // Remove this line.
    

    as dogCollection is not a std::vector<Dog>*.


    This is also incorrect:

    void Person::addDog(string dogName, string breed){
        Dog *newDog = new Dog(dogName, breed);
        dogCollection.push_back(newDog);
    }
    

    as dogCollection contains Dog instances, not Dog*. Change to:

    void Person::addDog(string dogName, string breed){
        dogCollection.push_back(Dog(dogName, breed));
    }
    

    There is a problem with all of constructors:

    Person::Person(string name, string address, int age){
        name=name;
        address=address;
        age=age;
    }
    

    This is assigning the argument name to itself: it is not assigning to the member name. Same for address and age and similarly for the constructors of the other classes. Use initializer list:

    Person::Person(string name, string address, int age) :
        name(name),
        address(address),
        age(age)
    {}
    

    This method does not return a std::string:

    string Person::getDogInfo(int index){
    }
    

    EDIT:

    Missing class qualifier:

    std::vector<Dog> getDogs(){
        return dogCollection;  //dogCollection undefined error here
    }
    

    means this is just a free function, with no association to class Person and therefore no access to dogCollection.

    Change to:

    std::vector<Dog> Person::getDogs(){
        return dogCollection;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.