• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ confusion

Pegun

Golden Member
I'm having a problem with the program I'm experimenting with. I'm trying to do a problem that will add and subtract complex numbers (real + imag*i). I'm getting an unresolved external symbol error within the add and subtract parts at the main function. Heres the code:

// Imaginary.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cmath"
#include "Complex.h"
float i;


/*class Complex
//note: complex.h
{
public:
Complex(int a=0, int b=0);
Complex add(Complex &,Complex &);
Complex sub(Complex &,Complex &);
void print();

private:
int real;
int imaginary;
};*/


int _tmain(int argc, _TCHAR* argv[])
{


i = pow(-1.,0.5);
Complex num1(2, 5), num2(7, 8), num3, num4 ;
num1.print();
num2.print();
num3.add(num1,num2); // num3=num1+num2
num3.print();
num4.sub(num1,num2); //num4= num1-num2
num4.print();

return 0;
}


Any help is appreciated.
 
I think its made so that the add function will automatically add the two numbers in it. Because when i do

Complex::add()
{
Complex& = Complex1& + Complex2&;
}

to define add it tells me the function is overloaded and only differs by return type.
 
Originally posted by: Pegun
I think its made so that the add function will automatically add the two numbers in it. Because when i do

Complex::add()
{
Complex& = Complex1& + Complex2&;
}

to define add it tells me the function is overloaded and only differs by return type.

The function doesn't "automatically" do anything - you need to pass in the arguments to work with and then explicitly perform the required operations.

The function defintion here doesn't make any sense. Where are Complex1 and Complex2 coming from? You can't assign to a class name (Complex). And what are the ampersands about??

Where is the above code located?
 
*class Complex
//note: complex.h
{
public:
Complex(int a=0, int b=0);
Complex add(Complex1&, Complex2&);
Complex sub(Complex1&, Complex2&);
void print();

private:
int real;
int imaginary;
};*/


Complex::add()
{
Complex& = Complex1& + Complex2&;
}

Thus I'm trying to add the two input values together. I'm really confused if you cant tell already. I guess what i want to do is basically take a number (a,b) and pass it into complex add(complex1&, complex2&) where a and b will become complex 1 and 2 respectively. Then I want to add another set to calculate and complex number of a + bi form.
 
Originally posted by: Pegun

Ok, let's start at the beginning. This is a class declaration - you are setting out the basic structure of the Complex class. First off, you don't need that * before class. Simply

class Complex

*class Complex
//note: complex.h
{
public:
Complex(int a=0, int b=0);
Complex add(Complex1&, Complex2&);

Ok, what do you want this add() function to do? It looks as though you want to add the two arguments and return the result - if that's true, this should be a friend function rather then a member function. You see, all the functions defined here need to be called in connection with an instance of the Complex class. But given what I've just described, the calling instance wouldn't be involved at all in the operation to be performed. Let's instead say you want to add the two arguments and put the result into the calling instance and return nothing.

First what is Complex1 and Complex2?? You need to specify the type of the argument first - this is just Complex. Then you specify the argyument name, possibly with pointer or reference notation. A single number is not a valid variable name. So what you want here is:

void add(Complex a, Complex b);

Now, passing these arguments as references is a good idea - keeps you from amking copies of the arguments, so do this:

void add(Complex &a, Complex &b);

You should guarentee that the function won't change these arguments though, as such:

void add(const Complex &a, const Complex &b);

That's what your function declaration should look like. Similarly for sub()

Complex sub(Complex1&, Complex2&);
void print();

private:
int real;
int imaginary;
};*/

Ok, your class declaration is complete now. Though I have no idea what that */ is doing at the end - get rid of it.

Now you need to implement all the functions you declared above. You do this in another file, probably called complex.cc Include complex.h in it. Then the implementation of the add function would look something like this:

void Complex::add(const Complex &a, const Complex &b)
{
real = a.real + b.real;
imaginary = a.imaginary + b.imaginary;

return;
}

Similarly for your other functions including the constructor!
Complex::add()
{
Complex& = Complex1& + Complex2&;
}

Thus I'm trying to add the two input values together. I'm really confused if you cant tell already. I guess what i want to do is basically take a number (a,b) and pass it into complex add(complex1&, complex2&) where a and b will become complex 1 and 2 respectively. Then I want to add another set to calculate and complex number of a + bi form.

Using this class would then look something like this:

Complex A(3,4), B(5,6), C;

C.add(A, B); //C now contains the sum of A and B
 
uh based on your trying to add numbers with a bunch of ampersands in it, i think you probably need to read a tutorial on how to define classes first.

C++ is not a bad language, it is very powerful. just dont write the entire program first and then start debugging it. you really should have unit tested all those functions before proceeding, it is much harder for us to help you if basically everything is jacked up.


you probably want to not use an Add function. jus toverload the plus operator.
 
Back
Top