CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2013
    Posts
    44

    Weird Error C2679

    Hi !

    I defined a class :

    Code:
    class A
    {
    public:
           enum : char { VA, VB, VC };
    };
    and another one :

    Code:
    class B
    {
    A location;
    };
    In the file B.cpp, when I write :

    location = A::VA;


    I get an error C2679 binary '=' no operator found ...
    Why ?
    THanks for your help.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird Error C2679

    In the file B.cpp, when I write :

    location = A::VA;
    Post a real program that uses that line. According to the above B class, there is no way that line could even exist since the B class has no member functions, and "location" is a private member variable, unless "location" is some other variable with the same name.

    So if that "location" is the same one in class B, then what type of variable is "location"? It is an A. What type is A::VA? It is a char. So how do you convert a char to an "A"? That is what the compiler is complaining about.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2013
    Posts
    44

    Re: Weird Error C2679

    Here are the files :

    A.h

    #ifndef A_H
    #define A_H

    class A
    {
    public:
    enum {VA,VB,VC};
    };

    #endif // A_H


    B.h

    #ifndef B_H
    #define B_H

    #include "a.h"

    class B
    {
    private:
    A location;
    public:
    B();
    };

    #endif // B_H


    B.cpp

    #include "b.h"

    B::B()
    {
    location = A::VA; // Error here
    }


    C:\Users\PR_USER\Test\b.cpp:5: error: C2679: binary '=' : no operator found which takes a right-hand operand of type '' (or there is no acceptable conversion)
    c:\users\pr_user\test\a.h(8): could be 'A &A:perator =(const A &)'
    while trying to match the argument list '(A, )'

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird Error C2679

    Quote Originally Posted by mulfycrowh View Post
    Here are the files :
    When posting code, please use code tags. This way, the code becomes formatted.

    Second, the question still is "how do you convert an int to an A object?". That is what that the compiler is complaining about. Maybe you should tell us what that line is supposed to do that is causing the error, since it doesn't make sense to me (or the compiler).

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2013
    Posts
    44

    Re: Weird Error C2679

    Sorry for the tags I was looking for #.
    location is defined to be A type and A::VA too. So where is the problem ? Should I use overloaded operator ?

    Regards

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird Error C2679

    Quote Originally Posted by mulfycrowh View Post
    Sorry for the tags I was looking for #.
    location is defined to be A type and A::VA too. So where is the problem ? Should I use overloaded operator ?

    Regards
    I still don't understand what you're trying to do. Forget about syntax -- on a high-level, what are you trying to achieve with that line of code?

    A::VA is an enumerated constant, and the type for enumerated constants is int. The location member is an A, and an A type is not an int. So you need to tell us what does it mean to convert an int to an A type before embarking on writing any code.

    No one can tell you what it means, not us, or the compiler, because an int and an A type are unrelated types. It is equivalent to asking us how to convert a Car to an Elephant type -- that's why we can't help further until you tell us (and the compiler) what it means to convert a car to an elephant (or in your case, an int to an "A" object).

    If you don't know, then you shouldn't be doing this.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 20th, 2013 at 01:08 PM.

  7. #7
    Join Date
    Jan 2013
    Posts
    44

    Re: Weird Error C2679

    I thought that VA is A type and that's I want. I cannot use enum class ... because I use VS2010

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird Error C2679

    Quote Originally Posted by mulfycrowh View Post
    I thought that VA is A type and that's I want.
    Which C++ documentation stated that VA is an A type? An enum is an integer type.

    And even if this is what you want, I still don't understand what you're trying to achieve. Why are you placing "A" objects inside the "A" class? What design are you trying to emulate? Maybe if you tell us on a high-level what you're trying to do, then we can show you one or more proper ways of achieving this goal.

    Regards,

    Paul McKenzie

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

    Re: Weird Error C2679

    A is a class which contains an enum which can take one of three values which have a type of int.

    B is a class which contains a private member location which is of type class A.

    To do what you want you'll need to provide an operator=() function for class A which takes an int. You might also want a constructor for A which also takes an int.

    If all you want is to hold a location in class B that can only take one of three values, then you could do

    Code:
    class A
    {
    public:
    	typedef enum {VA,VB,VC} loc;
    };
    
    class B
    {
    private:
    	A::loc location;
    
    public:
    	B();
    };
    
    B::B()
    {
    	location = A::VA;
    }
    Last edited by 2kaud; October 20th, 2013 at 02:42 PM.
    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)

  10. #10
    Join Date
    Jan 2013
    Posts
    44

    Re: Weird Error C2679

    Thank you very much for your explanation 2kaud.
    What I would like is the same as enum class that you can write in VS2012. It's interesting because you can manage same names in enumeration.
    Note: as I hover the mouse over VA it displays A VA

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird Error C2679

    Quote Originally Posted by mulfycrowh View Post
    Thank you very much for your explanation 2kaud.
    What I would like is the same as enum class that you can write in VS2012.
    Well that can't happen. The reason is that VS 2012 is based on the ANSI C++ 11 standard. Any previous compilers are based on ANSI 2003 C++ and C++ 98. Enum types do not exist in ANSI C++ 2003 and prior.

    The bottom line is that if you're used to using syntax that originates with C++ 11, then you better be an intermediate to advanced C++ programmer to emulate that same construct in any version prior to C++ 11. This could mean anything from writing code that's more tedious, to coming up with a new design altogether.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 20th, 2013 at 02:54 PM.

  12. #12
    Join Date
    Jan 2013
    Posts
    44

    Re: Weird Error C2679

    As I follow what 2kaud said, it's OK. Thanks a lot.

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