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

Thread: example program

  1. #1
    Join Date
    May 2015
    Posts
    500

    example program

    Hello ,

    I am trying to solve the following example program. The code below is already there.
    What i am confused , is they have defined struct template, without defining the struct itself.

    Code:
    #include <iostream>
    using namespace std;
    enum class Fruit { apple, orange, pear };
    enum class Color { red, green, orange };
    
    template <typename T> struct Traits;
    
    int main()
    {
    	int t = 0; std::cin >> t;
    
        for (int i=0; i!=t; ++i) {
            int index1; std::cin >> index1;
            int index2; std::cin >> index2;
            cout << Traits<Color>::name(index1) << " ";
            cout << Traits<Fruit>::name(index2) << "\n";
        }
    }
    I am unable to change the above red line of code to add actual struct ! I mean, the above line looks like declaration, but i want to add actual definition !!!

    Please may be i am missing something. Please help me with suggestions

    thanks
    pdk
    Last edited by pdk5; November 24th, 2020 at 04:10 AM.

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

    Re: example program

    This code doesn't compile with VS2019. There's a definition of Traits missing as the static function name() is used but not defined. Assuming name() is supposed to return a string with the equivalent fruit name, then these strings need to be defined somewhere. You can't return the string "apple" from the enum constant apple without somewhere being an association.
    Last edited by 2kaud; November 24th, 2020 at 05:48 AM.
    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
    May 2015
    Posts
    500

    Re: example program

    Thanks a lot kaud for the response.
    Yes, it was incomplete code, and i am supposed to add missing part as part of exercise.
    But my doubt was, why they are using template declaration !!!.

    I finally managed to fill the code in and gives the expected output !.
    Code:
    #include <iostream>
    using namespace std;
    enum class Fruit { apple, orange, pear };
    enum class Color { red, green, orange };
    
    template <typename T> struct Traits;
    
    template<>
    struct Traits<Fruit>{
        static string name(int index)
        {
            switch(index)
            {
                case static_cast<int>(Fruit::apple):
                return "apple";
                break;
                case static_cast<int>(Fruit::orange):
                return "orange";
                break;
                case static_cast<int>(Fruit::pear):
                return "pear";
                break;
                default:
                return "unknown";
                break;
            }
            
        }
    };
    
    template<>
    struct Traits<Color>{
        static string name(int index)
        {
            switch(index)
            {
                case static_cast<int>(Color::red):
                return "red";
                break;
                case static_cast<int>(Color::green):
                return "green";
                break;
                case static_cast<int>(Color::orange):
                return "orange";
                break;
                default:
                return "unknown";
                break;
            }
            
        }
    };
    // Define specializations for the Traits class template here.
    
    
    int main()
    {
    	int t = 0; std::cin >> t;
    
        for (int i=0; i!=t; ++i) {
            int index1; std::cin >> index1;
            int index2; std::cin >> index2;
            cout << Traits<Color>::name(index1) << " ";
            cout << Traits<Fruit>::name(index2) << "\n";
        }
    }
    thankyou
    pdk

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

    Re: example program

    Traits is a templated struct. It does nothing but introduce the Traits structure.

    Specialised templates are then used for type Fruit and type Color. If the templated type is Fruit then the Fruit specialization of Traits is used. Same if the templated type is Color then the Color specialization of Traits is used.

    Consider:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    enum class Fruit { apple, orange, pear };
    enum class Color { red, green, orange };
    
    template <typename T> struct Traits {
    	static string name(T index)
    	{
    		return "!" + to_string(index) + "!";
    	}
    };
    
    template<>
    struct Traits<Fruit> {
    	static string name(int index)
    	{
    		switch (index)
    		{
    			case static_cast<int>(Fruit::apple) :
    				return "apple";
    				break;
    
    			case static_cast<int>(Fruit::orange) :
    				return "orange";
    				break;
    
    			case static_cast<int>(Fruit::pear) :
    				return "pear";
    				break;
    
    			default:
    				return "fruit unknown";
    				break;
    		}
    
    	}
    };
    
    template<>
    struct Traits<Color> {
    	static string name(int index)
    	{
    		switch (index)
    		{
    			case static_cast<int>(Color::red) :
    				return "red";
    				break;
    
    			case static_cast<int>(Color::green) :
    				return "green";
    				break;
    
    			case static_cast<int>(Color::orange) :
    				return "orange";
    				break;
    
    			default:
    				return "color unknown";
    				break;
    		}
    
    	}
    };
    
    int main()
    {
    	int t = 0;
    	std::cin >> t;
    
    	for (int i = 0; i != t; ++i) {
    		int index1; std::cin >> index1;
    		int index2; std::cin >> index2;
    		int index3; std::cin >> index3;
    
    		cout << Traits<int>::name(index3) << ' ';
    		cout << Traits<Color>::name(index1) << " ";
    		cout << Traits<Fruit>::name(index2) << "\n";
    	}
    }
    where a body for Traits has been provided - which is used if T is not Color and T is not Fruit
    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
    May 2015
    Posts
    500

    Re: example program

    Thankyou very much kaud.


    I donot know how my code worked (on that test site)without default template definition.

Tags for this Thread

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