Is there a way for a program to generate the name of an instance of a class?

This is a discussion on Is there a way for a program to generate the name of an instance of a class? within the C++ Programming forums, part of the General Programming Boards category; Alright, I did some reading and I think I'm starting to understand how to use references to get around global ...

  1. #16
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Alright, I did some reading and I think I'm starting to understand how to use references to get around global variables.

    Code:
    void addNewEntry(std::vector<db_entry>& data)
    {
        data.push_back(db_entry());
        cin >> data.back().value1 >> data.back().value2 >> data.back().value3;
    }
    This piece uses the & to make the argument a reference to the vector, so when I call the function I should use something like addNewEntry(data).. or what arguments exactly? I get that anywhere in the program I can go data[7].value1 to get to the value.. and I'm assuming references don't require a -> in place of a . like pointers do?

    The reason I thought a pointer was a global variable is cause when I wrote that I didn't understand it was going to be in function arguments and return values.

    I don't really use globals that much anyway, but without references in functions like that I wasn't sure how they'd have to be used at least a few times.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,020
    Yeah that's right. Here's another usage example:

    Code:
    #include <iostream>
    
    int main(){
      std::vector<db_entry> database;
      std::cout << "Enter a db entry";
      
      addNewEntry(database);
      std::cout << "you entered an entry with value1 = " << database.back().value1;
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    so when I call the function I should use something like addNewEntry(data)
    correct (assuming data is a vector containing db_entry items)
    and I'm assuming references don't require a -> in place of a . like pointers do?
    Correct (Unless you are dealing with a reference to a pointer )
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #19
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Okay, I've tried writing something, but now I have lots and lots of wonderful errors.

    global.h
    Code:
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    int validateMenuInput(int length);
    
    float getSalary(int hours, float pay);
    void addNewEntry(std::vector<database>& employee); //Errors are here
    float generateIdNumber();
    In here, it says on the line with void addNewEntry() that:
    "'database' was not declared in this scope"
    "template 1 argument is invalid"
    "template 2 argument is invalid"
    "ISO C++ forbids declaration of 'employee' with no type"

    Looks like it doesn't realize I declare database as a structure later.. How do I make sure the function declaration knows what database is?

    main.cpp
    Code:
    int main()
    {
        srand((unsigned)time(0));
        struct database
        {
            int idNumber;
            int age;
            string name;
            string title;
            int hours;
            int wage;
            int salary;
        };
        std::vector<database> employee; //Errors are here
        addNewEntry(employee);
        cout<<"number: "<<employee[1].number<<"\n";
    }
    I tried to sort out the errors that'll go away when addNewEntry() works properly.. That left me with some on the line that declares the vector:
    "'main()::database' uses local type 'main()::database'"
    "trying to instantiate `template<class _Alloc> class std::allocator'"
    "template argument 2 is invalid"
    "invalid type in declaration before ';' token"

    addNewEntry() function:
    Code:
    void addNewEntry(std::vector<database>& employee)
    {
        employee.push_back(database());
        employee.back().idNumber = generateIdNumber();
        cout<<"ID Number: "<<employee.back().idNumber;
        cout<<"Enter the employee's name: ";
        cin>>employee.back().name;
        cout<<"Enter the employee's age: ";
        cin>>employee.back().age;
        cout<<"Enter the employee's title: ";
        cin>>employee.back().title;
        cout<<"Enter the employee's wage: ";
        cin>>employee.back().wage;
        cout<<"Enter the employee's weekly hours: ";
        cin>>employee.back().hours;
        employee.back().salary = getSalary(employee.back().hours, employee.back().wage);
        cout<<"Employee's salary: "<<employee.back().salary;
    }
    I just included this in case it's important. generateIdNumber() just returns a number between 10001 and 98304, and getSalary() calculates monthly salary with overtime wages for more than 40 hours a week. I doubt they're causing the problem.


    Like most errors I'm guessing this is all because of one thing, which I guess is that the program doesn't realize the vector is for database, especially in the declaration for addNewEntry()

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by verxintRising View Post
    Okay, I've tried writing something, but now I have lots and lots of wonderful errors.

    global.h
    Code:
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    int validateMenuInput(int length);
    
    float getSalary(int hours, float pay);
    void addNewEntry(std::vector<database>& employee); //Errors are here
    float generateIdNumber();
    In here, it says on the line with void addNewEntry() that:
    "'database' was not declared in this scope"
    "template 1 argument is invalid"
    "template 2 argument is invalid"
    "ISO C++ forbids declaration of 'employee' with no type"

    Looks like it doesn't realize I declare database as a structure later.. How do I make sure the function declaration knows what database is?
    Move the struct to your header file before the place you use it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. instance of a class disappearing after function ends
    By combatdave in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2006, 08:59 AM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21