How to retrieve the user defined data type into main function
-
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
{
templatetypedef struct {
Cryptography::EllipticCurve::Point Pa;
Cryptography::FiniteFieldElementc1;
Cryptography::FiniteFieldElementc2;
} 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_tmy_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_tmy_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"
-
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
{
templatetypedef struct {
Cryptography::EllipticCurve::Point Pa;
Cryptography::FiniteFieldElementc1;
Cryptography::FiniteFieldElementc2;
} 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_tmy_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_tmy_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"
-
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
{
templatetypedef struct {
Cryptography::EllipticCurve::Point Pa;
Cryptography::FiniteFieldElementc1;
Cryptography::FiniteFieldElementc2;
} 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_tmy_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_tmy_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"
-
Quote:
I am getting so many errors
I would start fixing them (or post them here if you are unable to).
Veni, vidi, vici.
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
-
Templates and namespaces are very important for my program. I have not posted the full code there...
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
-
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
-
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
{
templatetypedef struct {
Cryptography::EllipticCurve::Point Pa;
Cryptography::FiniteFieldElementc1;
Cryptography::FiniteFieldElementc2;
} 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_tmy_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_tmy_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"
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 thetemplate <argument list>
. Last: fix yourmain()
. 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 anint
. 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 formain()
(alwaysint
), and yourmain
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. -
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 thetemplate <argument list>
. Last: fix yourmain()
. 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 anint
. 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 formain()
(alwaysint
), and yourmain
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.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
-
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
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.
-
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.
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
-
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
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...
-
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
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. -
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. -
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);
}