Thread: New silly newb questoin. Returning vector from a class

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    New silly newb questoin. Returning vector from a class

    Hi all,

    Sorry for this, but it's driving me insane now, been at it for hours. Been searching the boards and google for similiar errors or even code examples of returning vectors, but not seeing anything similiar. Building on my last project of generating stars and planets, I decided to use a class to generate a vector filled with star names to associate with the stars I generated.

    The problem I'm running into, is that I keep getting the compiler error " 'vector' does not name a type".

    I realize(I think) that my problem is that the compiler is looking for a datatype to return from my method, but for the life of me I can't figure out what I should be placing there.

    I'm guessing that it doesn't associate vector as a datatype, but rather a container, and wants to know what it contains. Is that right?

    Anyways, the code I'm using is this...

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Names
    {
          public:
                 
                 Names()
                 {
                  vector<string> name_getter;
                        
                     name_getter.push_back("Acrux");
                     name_getter.push_back("Alcor");
                     ........
                     name_getter.push_back("Yildun");
                     name_getter.push_back("Zosma");
                 }
                 
                 ~Names();            
    }
     
    vector Names :: getNames()
     {
        return name_getter;
     }
    If anyone could please toss me a hint how to return my vector?

    Again, any help is much appriciated, Hope I'm not being annoying.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Code:
    vector<string> Names :: getNames()
    BTW, there are other problems
    Post if you need help with those too
    Last edited by MadCow257; 03-26-2005 at 05:45 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Ty!

    (Scurries off back to work!)

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Ty again for your offer Madcow, I took a little break and firgured out that ; is my friend and I should put it at the end of classes Also added the function getNames to the class and now it compiles fine.

    Question:

    In C++ does the class have to be part of the code of the main program for it to compile correctly, or can the class be a seperate file?

    I ask because I created it as a seperate file, and I can declare an instance of it, the compiler will even automatically pull up the function getNames, but when I try to compile it tells me

    "Names undeclared first use this function"

    error on the line I declare my instance.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Code:
    In C++ does the class have to be part of the code of the main program for it to compile correctly, or can the class be a seperate file?
    Sure, you can put it in a header file

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    did you include the header file you saved the functions in?
    Code:
    #include "some_file_with_code_and_dot_h_extension.h"
    please excuse the long name, but it gets the point across
    Last edited by xviddivxoggmp3; 03-26-2005 at 07:52 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Nope,

    That was what I was missing!

    I hadn't realized in C++ that external classes needed saved in a different format, a valueable lesson learned today.

    Thanks guys for all your help!

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    They don't need to be in a different format. All files that you write are simply plain-text files. The file extensions are just convention:

    Header files: where you have your class and function declarations typically.
    Extensions are often: .h .hh .hpp .hxx

    Source files: where you have the definition of your classes and functions typically.
    Extensions are often: .cpp .cc .C .cxx
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    28
    Header files: where you have your class and function declarations typically.
    Extensions are often: .h .hh .hpp .hxx

    Source files: where you have the definition of your classes and functions typically.
    Extensions are often: .cpp .cc .C .cxx
    Careful with that. Some compilers will decide whether to compile a given input as C or C++ based on the extension...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM
  4. Class accessor functions returning strings?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-31-2001, 12:48 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM