CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Classes

  1. #1
    Join Date
    Jun 2015
    Posts
    175

    Classes

    Hi guys,

    The following code is a small part of a program I want to write for a question. The question says this: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.

    I wrote the code below for it but face an error saying: identifier "_n" is undefined. But I have defined it in D(int _i, string _n).

    What is the reason please? An how to solve it?

    Code:
    #include <iostream>
    using namespace std;
    
    class B {
    public:
    	B(string n) : name(n) {}
    	string name;
    };
    
    //--------------------------
    
    class D : public B {
    public:
    	D(int _i, string _n) : B(_n),i(_i) {}
    	
    	int i;
    	B b1(_n);
    };
    
    //--------------------
    
    int main() {
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Classes

    Code:
       B b1(_n);
    Why do you think that _n is defined here when it is only a parameter for the constructor?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2015
    Posts
    175

    Re: Classes

    OK. I changed class D with this:
    Code:
    class D : public B {
    public:
    	D(int _i, string _n) : B(_n), i(_i), nd(_n) {}
    	
    	int i;
    	string nd;
    	B b1(nd);
    };
    But now get these:
    Error 1 error C2061: syntax error : identifier 'nd' 18
    2 IntelliSense: member "D::nd" is not a type name 18

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Classes

    Correct! The error message should give a clue. If you saw the statement
    Code:
    B b1(int);
    what would you think it was?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2015
    Posts
    175

    Re: Classes

    I don't know what you mean really but B b1(int); is not correct because the class B gets an variable for its objects not a type. As far as it seems to me B b1(nd); is correct because nd is a variable string, what the class B needs to its objects.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Classes

    Code:
    B b1(int);
    look like a function declaration! So to the compiler
    Code:
    B b1(nd);
    also looks like a function declaration with an unknown type for the parameter - hence the error message regarding unknown type !

    Try
    Code:
    B b1 = B(nd);
    Similarly, your original class D could be
    Code:
    class D : public B {
    public:
    	D(int _i, string _n) : B(_n), i(_i) {}
    
    	int i;
    	B b1 = B(name);
    };
    PS I don't know what compiler you are using, but with universal initialisation you could do
    Code:
       B b1 {name};
    or
    Code:
       B b1 {nd};
    Last edited by 2kaud; January 18th, 2016 at 05:47 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Classes

    FWIW, you assignment doesn't say anything about deriving D from B.

  8. #8
    Join Date
    Jun 2015
    Posts
    175

    Re: Classes

    Thank you 2Kuad.
    B b1(nd); I don't know why it should look like a function declaration! nd is already defined.
    B b1 = B(nd); seems odd to me. I have studied about class derivations but I don't recall that kind of making an object. Likely I've forgotten it. I think I should review that section of my book.

    @GCDEF: Thanks for your advice. Would you please explain more?

    And another question please:
    If you had the question saying the following, what simplest code would you write for it? I used derivation method because I thought it's the only possible way (for having a value of a class as a member of another class) and it's also simple.
    Question: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Classes

    Quote Originally Posted by tomy12 View Post
    Thank you 2Kuad.
    B b1(nd); I don't know why it should look like a function declaration! nd is already defined.
    B b1 = B(nd); seems odd to me. I have studied about class derivations but I don't recall that kind of making an object. Likely I've forgotten it. I think I should review that section of my book.

    @GCDEF: Thanks for your advice. Would you please explain more?

    And another question please:
    If you had the question saying the following, what simplest code would you write for it? I used derivation method because I thought it's the only possible way (for having a value of a class as a member of another class) and it's also simple.
    Question: Create a class named B with a data member (e.g., a string) and another class named D with its own data member (say, another string) and that class D holds a value of class B.
    There's not much more to say. You've derived D from B as in
    class D : public B
    which means D is derived from B. Your assignment doesn't say to do that. All you need is
    class D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured