Passing Through Constructor - Simple Error

Discussion in 'C++' started by GRoll35, Jan 25, 2006.

  1. GRoll35

    GRoll35 Guest

    I have 3 files here - Header/Implementation/Driver

    All it has to do is send the user's input (age)..to the class and the
    class will figure out the price of the ticket. I'm suppose to create
    the object and then have it display the cost of the ticket. I'm new to
    using classes like this so I'm a little confused. Here is the errors I
    get.

    If anyone could point out where to start or something that I should
    keep in mind that would be very appreciated! Thanks!

    Errors: (all in the Driver)

    Driver.cpp(18): error C3861: 'myImp': identifier not found, even with
    argument-dependent lookup
    Driver.cpp(16): error C3861: 'myImp': identifier not found, even with
    argument-dependent lookup
    Driver.cpp(18): error C2228: left of '.getAge' must have
    class/struct/union type type is ''unknown-type''
    Driver.cpp(16): error C2146: syntax error : missing ';' before
    identifier 'myImp'
    Driver.cpp(16): error C2065: 'Imp' : undeclared identifier

    Header:

    #ifndef HEADER_H
    #define HEADER_H

    class Header
    {
    private:
    int age;
    int ticket;

    public:
    Header();
    Header(int);
    int getAge();
    };

    #endif


    Implantation:

    #include "header.h"

    Header::Header(int age1)
    {
    int age = age1;

    }

    int Header::getAge()
    {
    if(age < 5)
    {
    ticket = 0;
    }
    else if (age < 18)
    {
    ticket = 5;
    }
    else if (age < 56)
    {
    ticket = 10;
    }
    else if (age > 55)
    {
    ticket = 8;
    }

    return ticket;
    }

    Driver

    #include <iostream>
    #include "Imp.cpp"

    using namespace std;

    int main()
    {
    int userAge = 0;

    cout << "Please Enter your age: ";
    cin >> userAge;


    Imp myImp(userAge);
    cout << "\n";
    cout << "Cost of Movie Ticket: " << myImp.getAge() << endl;
    cout << "\n\n";
    return 0;

    }:
    GRoll35, Jan 25, 2006
    #1
    1. Advertising

  2. GRoll35 wrote:
    > I have 3 files here - Header/Implementation/Driver
    >
    > All it has to do is send the user's input (age)..to the class and the
    > class will figure out the price of the ticket. I'm suppose to create
    > the object and then have it display the cost of the ticket. I'm new to
    > using classes like this so I'm a little confused. Here is the errors I
    > get.
    >
    > If anyone could point out where to start or something that I should
    > keep in mind that would be very appreciated! Thanks!
    >
    > Errors: (all in the Driver)
    >
    > Driver.cpp(18): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(16): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(18): error C2228: left of '.getAge' must have
    > class/struct/union type type is ''unknown-type''
    > Driver.cpp(16): error C2146: syntax error : missing ';' before
    > identifier 'myImp'
    > Driver.cpp(16): error C2065: 'Imp' : undeclared identifier
    >
    > Header:
    >
    > #ifndef HEADER_H
    > #define HEADER_H
    >
    > class Header
    > {
    > private:
    > int age;
    > int ticket;
    >
    > public:
    > Header();
    > Header(int);
    > int getAge();
    > };
    >
    > #endif
    >
    >
    > Implantation:
    >
    > #include "header.h"
    >
    > Header::Header(int age1)
    > {
    > int age = age1;
    >
    > }
    >
    > int Header::getAge()
    > {
    > if(age < 5)
    > {
    > ticket = 0;
    > }
    > else if (age < 18)
    > {
    > ticket = 5;
    > }
    > else if (age < 56)
    > {
    > ticket = 10;
    > }
    > else if (age > 55)
    > {
    > ticket = 8;
    > }
    >
    > return ticket;
    > }
    >
    > Driver
    >
    > #include <iostream>
    > #include "Imp.cpp"
    >
    > using namespace std;
    >
    > int main()
    > {
    > int userAge = 0;
    >
    > cout << "Please Enter your age: ";
    > cin >> userAge;
    >
    >
    > Imp myImp(userAge);
    > cout << "\n";
    > cout << "Cost of Movie Ticket: " << myImp.getAge() << endl;
    > cout << "\n\n";
    > return 0;
    >
    > }:




    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Jan 25, 2006
    #2
    1. Advertising

  3. GRoll35 wrote:
    > I have 3 files here - Header/Implementation/Driver
    >
    > All it has to do is send the user's input (age)..to the class and the
    > class will figure out the price of the ticket. I'm suppose to create
    > the object and then have it display the cost of the ticket. I'm new to
    > using classes like this so I'm a little confused. Here is the errors I
    > get.
    >
    > If anyone could point out where to start or something that I should
    > keep in mind that would be very appreciated! Thanks!
    >
    > Errors: (all in the Driver)
    >
    > Driver.cpp(18): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(16): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(18): error C2228: left of '.getAge' must have
    > class/struct/union type type is ''unknown-type''
    > Driver.cpp(16): error C2146: syntax error : missing ';' before
    > identifier 'myImp'
    > Driver.cpp(16): error C2065: 'Imp' : undeclared identifier
    >
    > Header:
    >
    > #ifndef HEADER_H
    > #define HEADER_H
    >
    > class Header


    So, your class is called "Header".


    > {
    > [...]
    > };
    >
    > #endif
    >
    >
    > Implantation:
    >
    > #include "header.h"
    >
    > Header::Header(int age1)


    And you implement a class called "Header".

    > {
    > [...]
    > }
    >
    > Driver
    >
    > #include <iostream>
    > #include "Imp.cpp"
    >
    > using namespace std;
    >
    > int main()
    > {
    > int userAge = 0;
    >
    > cout << "Please Enter your age: ";
    > cin >> userAge;
    >
    >
    > Imp myImp(userAge);


    So, what the hell is "Imp"? You declare 'myImp' to be of type
    'Imp'. There is no such type in your program.

    > cout << "\n";
    > cout << "Cost of Movie Ticket: " << myImp.getAge() << endl;
    > cout << "\n\n";
    > return 0;
    >
    > }:


    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Jan 25, 2006
    #3
  4. GRoll35

    GRoll35 Guest

    yes you are right it made no sense. so i changed the class to move.
    then in the driver i created the object theMovies by doing this.

    move theMovies(userAge);

    so here is my header file. the bottom is the errors i get now.

    #ifndef HEADER_H
    #define HEADER_H

    class move
    {
    private:
    int age;
    int ticket;

    public:
    move();
    move(int);
    int getAge();
    };

    #endif


    Movie1 fatal error LNK1169: one or more multiply defined symbols found
    Movie1 error LNK2005: "public: int __thiscall move::getAge(void)"
    (?getAge@move@@QAEHXZ) already defined in Driver.obj
    Movie1 error LNK2005: "public: __thiscall move::move(int)"
    (??0move@@QAE@H@Z) already defined in Driver.obj

    any idea what thats saying. again, I appologize for my lack of
    knowledge and appreciate your help.
    GRoll35, Jan 25, 2006
    #4
  5. GRoll35 wrote:
    > yes you are right it made no sense. so i changed the class to move.
    > then in the driver i created the object theMovies by doing this.
    >
    > move theMovies(userAge);
    >
    > so here is my header file. the bottom is the errors i get now.
    >
    > #ifndef HEADER_H
    > #define HEADER_H
    >
    > class move
    > {
    > private:
    > int age;
    > int ticket;
    >
    > public:
    > move();
    > move(int);
    > int getAge();
    > };
    >
    > #endif
    >
    >
    > Movie1 fatal error LNK1169: one or more multiply defined symbols found
    > Movie1 error LNK2005: "public: int __thiscall move::getAge(void)"
    > (?getAge@move@@QAEHXZ) already defined in Driver.obj
    > Movie1 error LNK2005: "public: __thiscall move::move(int)"
    > (??0move@@QAE@H@Z) already defined in Driver.obj
    >
    > any idea what thats saying. again, I appologize for my lack of
    > knowledge and appreciate your help.


    You probably compiled your '.cc' files separately, and then linked them
    together to form your final program. But remember, you included the
    implementation file into the driver? You used #include, remember? Why?
    If you include the text of one source file into the other source file,
    do not compile the one you included, separately. Otherwise, do not
    include the other one, just compile them separately, then link.

    Get a good book that explains multi-file projects.


    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Jan 25, 2006
    #5
  6. GRoll35

    James Guest

    hello dear
    First clear your concepts of C++ taking gud book
    the following is not a correct way to initialising a member variable
    of class in a constructor
    Header::Header(int age1)
    {
    int age = age1;
    }
    should be corrected to
    this->age=age1;
    or simply
    age = age1;
    and in driver.cpp your class is header not IMP , also corrects its
    name
    James, Jan 25, 2006
    #6
  7. GRoll35

    Peter_Julian Guest

    "James" <> wrote in message
    news:...
    | hello dear
    | First clear your concepts of C++ taking gud book
    | the following is not a correct way to initialising a member variable
    | of class in a constructor
    | Header::Header(int age1)
    | {
    | int age = age1;
    | }
    | should be corrected to
    | this->age=age1;
    | or simply
    | age = age1;
    | and in driver.cpp your class is header not IMP , also corrects its
    | name
    |

    Actually, the ctor should be using an init list instead:

    Header::Header(int n) : age(n) { }
    Peter_Julian, Jan 25, 2006
    #7
  8. GRoll35

    Howard Guest

    "GRoll35" <> wrote in message

    > Errors: (all in the Driver)
    >
    > Driver.cpp(18): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(16): error C3861: 'myImp': identifier not found, even with
    > argument-dependent lookup
    > Driver.cpp(18): error C2228: left of '.getAge' must have
    > class/struct/union type type is ''unknown-type''
    > Driver.cpp(16): error C2146: syntax error : missing ';' before
    > identifier 'myImp'
    > Driver.cpp(16): error C2065: 'Imp' : undeclared identifier
    >


    > Driver
    >
    > #include <iostream>
    > #include "Imp.cpp"


    Why are you including a .cpp file? Usually, you #include the associated .h
    file, and simply include the .cpp file as part of your "project" (or compile
    it separately and link it into the executable, if you're doing command-line
    building). Try:

    #include "Imp.h"

    >
    > using namespace std;
    >
    > int main()
    > {
    > int userAge = 0;
    >
    > cout << "Please Enter your age: ";
    > cin >> userAge;
    >
    >
    > Imp myImp(userAge);


    At this point, I assume Imp is not defined. Is there an "Imp.h" file? If
    so, once you incude it above instead of Imp.cpp, this problem should go
    away.

    > cout << "\n";
    > cout << "Cost of Movie Ticket: " << myImp.getAge() << endl;
    > cout << "\n\n";
    > return 0;
    >
    > }:
    >


    -Howard
    Howard, Jan 25, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,027
    Patrick Kowalzick
    Jun 25, 2003
  2. Replies:
    6
    Views:
    1,100
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  4. aidy
    Replies:
    1
    Views:
    100
    Chris Hulan
    Jul 7, 2008
  5. ingoweiss
    Replies:
    4
    Views:
    200
    Julian Turner
    May 12, 2006
Loading...

Share This Page