K
K
kornietsbk2020-04-20 23:58:23
C++ / C#
kornietsbk, 2020-04-20 23:58:23

What is the correct way to use typedef?

If in a class using typedef I create an "alias" for some built-in type, and then I specify the "alias" as a return type or as an argument for any member method, then the compiler gives an error.
Example:

typedef unsigned int DigitArea; 
    typedef char Symbol;         
    typedef char* SymbolicArea;     
    class String final{
        private:
             
            SymbolicArea Literal;         
            DigitArea Length;               
            DigitArea Volume;

            DigitArea IdentifyLength(const SymbolicArea str);        
            DigitArea IdentifyLength(SymbolicArea str);

Mistake:

«DigitArea String::IdentifyLength(SymbolicArea)» cannot be overloaded with «DigitArea String::IdentifyLength(SymbolicArea)»
   19 |             DigitArea IdentifyLength(SymbolicArea str);
      |

Compiler: g++.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-04-21
@kornietsbk

Remove the penultimate line. The compiler is complaining about the duplicate method declaration, IdentifyLength,not the typedef. typedefBy the way, in the new code, you should use : instead of using: Or better yet, use the types from the default. For example:
using DigitArea = unsigned int;
using DigitArea = std::int64_t;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question