-4

Its very simple but Iam unable to remove error. I want to store integer value in complex variable.

int int2 =2;
comp *cal = int2;

Thanks

Annie
  • 17
  • 5
  • 6
    You don't want the `*`; that makes it a pointer, not a complex variable. And how is `comp` defined? If it has an implicit constructor that takes a numeric type, then `comp cal = int2;` should work. – Mike Seymour Jun 14 '13 at 10:48
  • again !..plz read FAQ before asking a questn . – Spandan Jun 14 '13 at 10:52
  • If you are lost on how to deal with complex numbers in C++, take a look at the `std::complex` class (http://www.cplusplus.com/reference/complex/complex/). – Matthieu Rouget Jun 14 '13 at 10:52

1 Answers1

2

If you're using standard complex numbers (that is, comp is an alias for std::complex<something>):

comp cal = int2;

If comp isn't a standard complex type, then we'll need to see how it's defined; there's no way to guess how it might work.

UPDATE: Regarding your comment: since it's a custom aggregate type, and assuming Q is the real part:

comp cal = {0, int2};

although you really should consider using standard types instead.

Regarding the rest of your comment: Why on earth do you have to use a pointer? That's insane. But if you really need one:

comp cal_ = {0, int2};
comp *cal = &cal_;

or if you need dynamic allocation for some reason (which you almost certainly don't; and if you do, you should be using smart pointers not raw pointers):

comp *cal = new comp{0, int2};
// and don't forget to delete it when you've finished with it
delete cal;

or if you're stuck with the pre-2011 language:

comp *cal = new comp;
cal->I = 0;
cal->Q = int2;

But to reiterate, whatever you think you have to do, you almost certainly don't want a pointer here.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • thanks for the reply, but I have to use comp * , not only comp. Any other technique. comp is defined as typedef struct { float I; float Q; } comp; – Annie Jun 14 '13 at 11:00
  • @Annie: OK, I've updated the answer to show how to initialise your type. But I'm sure you don't have to use `comp*`; that would be insane. – Mike Seymour Jun 14 '13 at 11:08
  • Thanks alot. I knew that was simple, but wasnt coming to mind. – Annie Jun 14 '13 at 11:11