Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to retrieve the user defined data type into main function

How to retrieve the user defined data type into main function

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmssecuritytutorial
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Manoj7390

    Hi everyone.. I have written the code for encryption algorithm. It has one user function header file also. Sample code is as shown below... I want return {Pa,C1,C2} in the program to main function. Please help me..

    #include

    #include

    #include

    using namespace std;

    #include

    #include "FiniteFieldElement.hpp"

    namespace Cryptography

    {
    template

    typedef struct {
    Cryptography::EllipticCurve

    ::Point Pa;
    Cryptography::FiniteFieldElement

    c1;
    Cryptography::FiniteFieldElement

    c2;
    } encrypt_data_t;

        class   EllipticCurve
    
        {
    
            public:
               
                typedef FiniteFieldElement
    

    ffe_t;

                class   Point
    
                {
    
                    friend  class   EllipticCurve
    

    ;

                    typedef FiniteFieldElement
    

    ffe_t;

                    ffe\_t  x\_;
    
                    ffe\_t  y\_;
    
                    EllipticCurve    \*ec\_;
    
                    }
    
                   
     		.....
    	.....
    	....
    	...
    
                };
               
    
                typedef EllipticCurve
    

    this_t;

                typedef class EllipticCurve
    

    ::Point point_t;

    namespace utils

    {

    }

    using namespace Cryptography;

    using namespace utils;

    template
    encrypt_data_t

    my_encrypt()

    {

    typedef EllipticCurve<263> ec\_t;
    
    ec\_t   myEllipticCurve(1,1);
    

    // Alice

    ec\_t::Point Pa = a\*G;  // public key
    
    
    // encrypt using Bob\`s key
    
    ec\_t::Point Pk = a\*Pb;
    
    ec\_t::ffe\_t c1( m1\*Pk.x() );
    
    ec\_t::ffe\_t c2( m2\*Pk.y() );
    
    cout << "Encrypted message from Alice to Bob = {Pa,c1,c2} = {" << Pa << ", " << c1 << ", " << c2 << "}\\n\\n";
    
     encrypt\_data\_t <263> ret;
     ret.Pa = Pa;
     ret.c1 = c1;
     ret.c2 = c2;
    
     return ret;  
    

    }

    main()
    {
    int result;
    template
    result = encrypt_data_t

    my_encrypt();
    }

    "I want to return {Pa,C1,C2} to the main program using structures... I am getting so many errors" "Please help me with this problem"

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Get rid of all your templates and namespaces, they serve no purpose.

    Use the best guess

    M 1 Reply Last reply
    0
    • L Lost User

      Get rid of all your templates and namespaces, they serve no purpose.

      Use the best guess

      M Offline
      M Offline
      Manoj7390
      wrote on last edited by
      #4

      Templates and namespaces are very important for my program. I have not posted the full code there...

      L 1 Reply Last reply
      0
      • CPalliniC CPallini

        Quote:

        I am getting so many errors

        I would start fixing them (or post them here if you are unable to).

        Veni, vidi, vici.

        M Offline
        M Offline
        Manoj7390
        wrote on last edited by
        #5

        These are the errors i am getting for the program Manoj1.cc:25:17: error: template class without a name Manoj1.cc:25:9: error: template declaration of ‘typedef’ Manoj1.cc:39:44: error: ‘P’ was not declared in this scope Manoj1.cc:39:45: error: template argument 1 is invalid Manoj1.cc:47:37: error: ‘EllipticCurve’ is not a template Manoj1.cc:47:51: error: ‘P’ was not declared in this scope Manoj1.cc:49:48: error: ‘P’ was not declared in this scope Manoj1.cc:49:49: error: template argument 1 is invalid Manoj1.cc:231:41: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:231:55: error: ‘P’ was not declared in this scope Manoj1.cc:243:59: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:243:73: error: ‘P’ was not declared in this scope Manoj1.cc:418:25: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:418:39: error: ‘P’ was not declared in this scope Manoj1.cc:420:31: error: ‘EllipticCurve’ is not a template Manoj1.cc:420:45: error: ‘P’ was not declared in this scope Manoj1.cc:521:36: error: ‘P’ was not declared in this scope Manoj1.cc:521:37: error: template argument 1 is invalid Manoj1.cc:525:36: error: ‘P’ was not declared in this scope Manoj1.cc:525:37: error: template argument 1 is invalid Manoj1.cc:533:64: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:549:40: error: ‘P’ was not declared in this scope Manoj1.cc:549:41: error: template argument 1 is invalid Manoj1.cc:551:40: error: ‘P’ was not declared in this scope Manoj1.cc:551:41: error: template argument 1 is invalid Manoj1.cc: In member function ‘Cryptography::EllipticCurve::Point Cryptography::EllipticCurve::Point::scalarMultiply(int, const Cryptography::EllipticCurve::Point&)’: Manoj1.cc:90:51: error: call of overloaded ‘Point(int, int, Cryptography::EllipticCurve&)’ is ambiguous Manoj1.cc:90:51: note: candidates are: Manoj1.cc:243:21: note: Cryptography::EllipticCurve::Point::Point(const ffe_t&, const ffe_t&, Cryptography::EllipticCurve&) Manoj1.cc:231:21: note: Cryptography::EllipticCurve::Point::Point(int, int, Cryptography::EllipticCurve&) Manoj1.cc: In member function ‘void Cryptography::EllipticCurve::Point::add(Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t&, Cryptography::EllipticCurve::Point::ffe_t&) const’: Manoj1.cc:179:40: error: request for member ‘i’ in ‘x1’, which is of non-class t

        L 1 Reply Last reply
        0
        • M Manoj7390

          Templates and namespaces are very important for my program. I have not posted the full code there...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Manoj7390 wrote:

          Templates and namespaces are very important for my program.

          That is most unlikely, and looking at what you have posted you are not using them for any good reason. You should get the code working first without the use of either, and then add them (if they are really necessary) at a later stage.

          Use the best guess

          1 Reply Last reply
          0
          • M Manoj7390

            These are the errors i am getting for the program Manoj1.cc:25:17: error: template class without a name Manoj1.cc:25:9: error: template declaration of ‘typedef’ Manoj1.cc:39:44: error: ‘P’ was not declared in this scope Manoj1.cc:39:45: error: template argument 1 is invalid Manoj1.cc:47:37: error: ‘EllipticCurve’ is not a template Manoj1.cc:47:51: error: ‘P’ was not declared in this scope Manoj1.cc:49:48: error: ‘P’ was not declared in this scope Manoj1.cc:49:49: error: template argument 1 is invalid Manoj1.cc:231:41: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:231:55: error: ‘P’ was not declared in this scope Manoj1.cc:243:59: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:243:73: error: ‘P’ was not declared in this scope Manoj1.cc:418:25: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:418:39: error: ‘P’ was not declared in this scope Manoj1.cc:420:31: error: ‘EllipticCurve’ is not a template Manoj1.cc:420:45: error: ‘P’ was not declared in this scope Manoj1.cc:521:36: error: ‘P’ was not declared in this scope Manoj1.cc:521:37: error: template argument 1 is invalid Manoj1.cc:525:36: error: ‘P’ was not declared in this scope Manoj1.cc:525:37: error: template argument 1 is invalid Manoj1.cc:533:64: error: ‘Cryptography::EllipticCurve’ is not a template Manoj1.cc:549:40: error: ‘P’ was not declared in this scope Manoj1.cc:549:41: error: template argument 1 is invalid Manoj1.cc:551:40: error: ‘P’ was not declared in this scope Manoj1.cc:551:41: error: template argument 1 is invalid Manoj1.cc: In member function ‘Cryptography::EllipticCurve::Point Cryptography::EllipticCurve::Point::scalarMultiply(int, const Cryptography::EllipticCurve::Point&)’: Manoj1.cc:90:51: error: call of overloaded ‘Point(int, int, Cryptography::EllipticCurve&)’ is ambiguous Manoj1.cc:90:51: note: candidates are: Manoj1.cc:243:21: note: Cryptography::EllipticCurve::Point::Point(const ffe_t&, const ffe_t&, Cryptography::EllipticCurve&) Manoj1.cc:231:21: note: Cryptography::EllipticCurve::Point::Point(int, int, Cryptography::EllipticCurve&) Manoj1.cc: In member function ‘void Cryptography::EllipticCurve::Point::add(Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t, Cryptography::EllipticCurve::Point::ffe_t&, Cryptography::EllipticCurve::Point::ffe_t&) const’: Manoj1.cc:179:40: error: request for member ‘i’ in ‘x1’, which is of non-class t

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            I think that confirms my comment to you about the use of namespaces and templates.

            Use the best guess

            1 Reply Last reply
            0
            • M Manoj7390

              Hi everyone.. I have written the code for encryption algorithm. It has one user function header file also. Sample code is as shown below... I want return {Pa,C1,C2} in the program to main function. Please help me..

              #include

              #include

              #include

              using namespace std;

              #include

              #include "FiniteFieldElement.hpp"

              namespace Cryptography

              {
              template

              typedef struct {
              Cryptography::EllipticCurve

              ::Point Pa;
              Cryptography::FiniteFieldElement

              c1;
              Cryptography::FiniteFieldElement

              c2;
              } encrypt_data_t;

                  class   EllipticCurve
              
                  {
              
                      public:
                         
                          typedef FiniteFieldElement
              

              ffe_t;

                          class   Point
              
                          {
              
                              friend  class   EllipticCurve
              

              ;

                              typedef FiniteFieldElement
              

              ffe_t;

                              ffe\_t  x\_;
              
                              ffe\_t  y\_;
              
                              EllipticCurve    \*ec\_;
              
                              }
              
                             
               		.....
              	.....
              	....
              	...
              
                          };
                         
              
                          typedef EllipticCurve
              

              this_t;

                          typedef class EllipticCurve
              

              ::Point point_t;

              namespace utils

              {

              }

              using namespace Cryptography;

              using namespace utils;

              template
              encrypt_data_t

              my_encrypt()

              {

              typedef EllipticCurve<263> ec\_t;
              
              ec\_t   myEllipticCurve(1,1);
              

              // Alice

              ec\_t::Point Pa = a\*G;  // public key
              
              
              // encrypt using Bob\`s key
              
              ec\_t::Point Pk = a\*Pb;
              
              ec\_t::ffe\_t c1( m1\*Pk.x() );
              
              ec\_t::ffe\_t c2( m2\*Pk.y() );
              
              cout << "Encrypted message from Alice to Bob = {Pa,c1,c2} = {" << Pa << ", " << c1 << ", " << c2 << "}\\n\\n";
              
               encrypt\_data\_t <263> ret;
               ret.Pa = Pa;
               ret.c1 = c1;
               ret.c2 = c2;
              
               return ret;  
              

              }

              main()
              {
              int result;
              template
              result = encrypt_data_t

              my_encrypt();
              }

              "I want to return {Pa,C1,C2} to the main program using structures... I am getting so many errors" "Please help me with this problem"

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #8

              Starting at the bottom, your main function doesn't make sense - it's one huge syntax error. Even with all the context and the realisation what this is about (encryption), it's almost impossible to guess what it should do. Obviously you're not familiar with template syntax. Your attempt to invoke a templated function using a templated type is a syntactical mess. Your data structure definitions contain errors related to incomplete or improper template syntax. For that reason, and because you don't really use or need those template parameters, you should really forget about them! Since you claim you do need the template, use a const instead: in most cases, where people think they need a template, a global/static definition works just as well:

              const int P = 263;

              Start with your struct definition:

              struct encrypt_data_t {
              EllipticCurve<P>::Point Pa;
              FiniteFieldElement<P> c1;
              FiniteFieldElement<P> c2;
              };

              There: no typedef required, no template parameters required. Also, since the definition is within your namespace, no need to explicitely add the prefix "Cryptography::". Next, remove any remaining template parameter definition line in your code: if you always used P as the template parameter name, everything should compile after removing the template <argument list>. Last: fix your main(). I have no idea what your intention is, but you do call a function that returns a struct, and apparently try to assign that struct to an int. That doesn't make sense. Fix it, or at least remove that assignment, so you can find out and fix any remaining syntax errors. Besides, you omitted the return type for main() (always int), and your main didn't return a value. Both should be listed as errors or at the very least as warnings by your compiler. But it's possible your compiler didn't get that far due to it's inability to resolve previous spaghetti template syntax.

              M 1 Reply Last reply
              0
              • S Stefan_Lang

                Starting at the bottom, your main function doesn't make sense - it's one huge syntax error. Even with all the context and the realisation what this is about (encryption), it's almost impossible to guess what it should do. Obviously you're not familiar with template syntax. Your attempt to invoke a templated function using a templated type is a syntactical mess. Your data structure definitions contain errors related to incomplete or improper template syntax. For that reason, and because you don't really use or need those template parameters, you should really forget about them! Since you claim you do need the template, use a const instead: in most cases, where people think they need a template, a global/static definition works just as well:

                const int P = 263;

                Start with your struct definition:

                struct encrypt_data_t {
                EllipticCurve<P>::Point Pa;
                FiniteFieldElement<P> c1;
                FiniteFieldElement<P> c2;
                };

                There: no typedef required, no template parameters required. Also, since the definition is within your namespace, no need to explicitely add the prefix "Cryptography::". Next, remove any remaining template parameter definition line in your code: if you always used P as the template parameter name, everything should compile after removing the template <argument list>. Last: fix your main(). I have no idea what your intention is, but you do call a function that returns a struct, and apparently try to assign that struct to an int. That doesn't make sense. Fix it, or at least remove that assignment, so you can find out and fix any remaining syntax errors. Besides, you omitted the return type for main() (always int), and your main didn't return a value. Both should be listed as errors or at the very least as warnings by your compiler. But it's possible your compiler didn't get that far due to it's inability to resolve previous spaghetti template syntax.

                M Offline
                M Offline
                Manoj7390
                wrote on last edited by
                #9

                If I delete template parameter, then it is giving so many errors. I will post the full program to you please fix it. I want to return {Pa,c1,c2} from this program to another main() function. Please help me out.

                #include <cstdlib>
                #include <iostream>
                #include <vector>
                using std:: cout;
                using std:: cin;
                using std:: endl;
                using namespace std;
                #include <math.h>
                #include "FiniteFieldElement.hpp"

                namespace Cryptography
                {
                template<int P>
                class EllipticCurve
                {
                public:

                            typedef FiniteFieldElement<P> ffe\_t;
                             
                            class   Point
                            {
                                friend  class   EllipticCurve<P>;
                                typedef FiniteFieldElement<P> ffe\_t;
                                ffe\_t  x\_;
                                ffe\_t  y\_;
                                EllipticCurve    \*ec\_;
                                
                                void   addDouble(int m, Point& acc)
                                {        
                                    if ( m > 0 )
                                    {       
                                        Point r = acc; 
                                        for ( int n=0; n < m; ++n )
                                        {
                                            r += r;     // doubling step                          
                                        }
                                        acc = r;
                                    }        
                                }
                                
                                Point scalarMultiply(int k, const Point& a)
                                {
                                    Point acc = a;
                                    Point res = Point(0,0,\*ec\_);
                                    int i = 0, j = 0;
                                    int b = k;
                                    
                                    while( b )
                                    {
                                        if ( b & 1 )
                                        {
                                            addDouble(i-j,acc);
                                            res += acc;           
                                            j = i;  
                                        }
                                        b >>= 1;
                                        ++i;
                                    }
                                    return res;
                                }
                                
                                void    add(ffe\_t x1, ffe\_t y1, ffe\_t x2, ffe\_t y2, ffe\_t & xR, ffe\_t & yR) const
                                {
                                                          
                                    if ( x1 == 0 && y1 == 0
                
                S 1 Reply Last reply
                0
                • M Manoj7390

                  If I delete template parameter, then it is giving so many errors. I will post the full program to you please fix it. I want to return {Pa,c1,c2} from this program to another main() function. Please help me out.

                  #include <cstdlib>
                  #include <iostream>
                  #include <vector>
                  using std:: cout;
                  using std:: cin;
                  using std:: endl;
                  using namespace std;
                  #include <math.h>
                  #include "FiniteFieldElement.hpp"

                  namespace Cryptography
                  {
                  template<int P>
                  class EllipticCurve
                  {
                  public:

                              typedef FiniteFieldElement<P> ffe\_t;
                               
                              class   Point
                              {
                                  friend  class   EllipticCurve<P>;
                                  typedef FiniteFieldElement<P> ffe\_t;
                                  ffe\_t  x\_;
                                  ffe\_t  y\_;
                                  EllipticCurve    \*ec\_;
                                  
                                  void   addDouble(int m, Point& acc)
                                  {        
                                      if ( m > 0 )
                                      {       
                                          Point r = acc; 
                                          for ( int n=0; n < m; ++n )
                                          {
                                              r += r;     // doubling step                          
                                          }
                                          acc = r;
                                      }        
                                  }
                                  
                                  Point scalarMultiply(int k, const Point& a)
                                  {
                                      Point acc = a;
                                      Point res = Point(0,0,\*ec\_);
                                      int i = 0, j = 0;
                                      int b = k;
                                      
                                      while( b )
                                      {
                                          if ( b & 1 )
                                          {
                                              addDouble(i-j,acc);
                                              res += acc;           
                                              j = i;  
                                          }
                                          b >>= 1;
                                          ++i;
                                      }
                                      return res;
                                  }
                                  
                                  void    add(ffe\_t x1, ffe\_t y1, ffe\_t x2, ffe\_t y2, ffe\_t & xR, ffe\_t & yR) const
                                  {
                                                            
                                      if ( x1 == 0 && y1 == 0
                  
                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #10

                  Without the declaration of the class FiniteFieldElement, and without the error messages, I can't help you. I've successfully compiled that code by using the following dummy declaration:

                  template
                  class FiniteFieldElement {
                  public:
                  FiniteFieldElement(){}
                  FiniteFieldElement(int){}
                  int i() const {return 0;}
                  operator int() const {return 0;}
                  };

                  This means that either there is a problem with the actual class declaration, or you are not using it correctly.

                  M 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    Without the declaration of the class FiniteFieldElement, and without the error messages, I can't help you. I've successfully compiled that code by using the following dummy declaration:

                    template
                    class FiniteFieldElement {
                    public:
                    FiniteFieldElement(){}
                    FiniteFieldElement(int){}
                    int i() const {return 0;}
                    operator int() const {return 0;}
                    };

                    This means that either there is a problem with the actual class declaration, or you are not using it correctly.

                    M Offline
                    M Offline
                    Manoj7390
                    wrote on last edited by
                    #11

                    The code is working fine. I need to return those three variables i.e. {Pa,c1,c2} from that program to another main program. What should i have to do. I have used structure for that but its not working. Please tell how to return a three user defined data type to the main function

                    S 2 Replies Last reply
                    0
                    • M Manoj7390

                      The code is working fine. I need to return those three variables i.e. {Pa,c1,c2} from that program to another main program. What should i have to do. I have used structure for that but its not working. Please tell how to return a three user defined data type to the main function

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #12

                      Manoj7390 wrote:

                      The code is working fine.

                      Now, that is news! From your original posting:

                      Manoj7390 wrote:

                      I am getting so many errors

                      May be you should have been clearer from the start about this...

                      1 Reply Last reply
                      0
                      • M Manoj7390

                        The code is working fine. I need to return those three variables i.e. {Pa,c1,c2} from that program to another main program. What should i have to do. I have used structure for that but its not working. Please tell how to return a three user defined data type to the main function

                        S Offline
                        S Offline
                        Stefan_Lang
                        wrote on last edited by
                        #13

                        There can only be one int main(). You have to separate the code that does the calculations from the code that inquires the input. You have plenty of functions that pass values around, so I don't see why there should be a problem passing these values via a function interface.

                        M 1 Reply Last reply
                        0
                        • S Stefan_Lang

                          There can only be one int main(). You have to separate the code that does the calculations from the code that inquires the input. You have plenty of functions that pass values around, so I don't see why there should be a problem passing these values via a function interface.

                          M Offline
                          M Offline
                          Manoj7390
                          wrote on last edited by
                          #14

                          how to return those values using structure or by declaring class

                          S 1 Reply Last reply
                          0
                          • M Manoj7390

                            how to return those values using structure or by declaring class

                            S Offline
                            S Offline
                            Stefan_Lang
                            wrote on last edited by
                            #15

                            Change the struct definition to

                            template<int P> struct encrypt_data_t {
                            typename Cryptography::EllipticCurve<P>::Point Pa;
                            FiniteFieldElement<P> c1;
                            FiniteFieldElement<P> c2;
                            } ;

                            Then call this function like this:

                            int main(...) {
                            const int MY_P = 263;
                            ...
                            encrypt_data_t result = my_encrypt();
                            ...
                            }

                            Of course, you also have to fix your function code - the code you posted does not declare half the variables it uses. Or you could just skip that structure and instead pass these values back by reference[^]:

                            template
                            void my_encrypt(typename Cryptography::EllipticCurve<P>::Point& Pa,
                            FiniteFieldElement

                            & c1, FiniteFieldElement

                            & c2) {
                            ...
                            }
                            int main(...) {
                            const int MY_P = 263;
                            typename Cryptography::EllipticCurve::Point Pa;
                            FiniteFieldElement c1;
                            FiniteFieldElement c2;
                            my_encrypt(Pa, c1, c2);
                            }

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups