function to create pointers to objects stored in a vector?

I am currently writing a program involving complex numbers (converting between rectangular and polar... and eventually basic mathematical operations)

I am stuck on creating a global function that will take a pointer to an object of my class (Complex) and change the complex number stored in that object for its complex conjugate.

The main issue I am having is getting a pointer to an object, I am new to C++ and have been searching everywhere for a way to do this... I'm very confused, could somebody please just point me in the right direction... even if its just topic wise ? :)

Also do you need to change the declaration of the class Complex for this to work?

should i be creating a vector of pointers? or using new? or is there some other way i can store the pointers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main(){

	char type;
	double a;
	double b;
	vector<Complex> myNums;

	string line;
	ifstream myFile;
	myFile.open("numbers.txt.");
	
	while(!myFile.eof()) {

	myFile >> type >> a >> b;
	getline(myFile, line);

	Complex newNumber(type, a, b);
	newNumber.getType();
	myNums.push_back(newNumber);
	
	}

	cout << endl;

	system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef Complex_H  // if undefined = ifndef
#define Complex_H

class Complex {
private:
	char type;
	double a;
	double b;
	
public:

	Complex(std::string numberInformation);
	//Default Constructor
	Complex();
	//Overload Constructor
	Complex (char type, double a, double b);
	//Destructor
	~Complex();

	//Accessor functions
	char getType() const;
	double getA() const;
	double getB() const;

	//Mutator functions
	void setType(char);
	void setA(double);
	void setB(double);
	
	void getType();
	void convertPol();
	void convertRec();

	
};



#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include "Complex1.h"
#include <cmath>

using namespace std;
Complex::Complex (char which, double first, double last){
	type = which;
	a = first;
	b = last;
}



Complex::~Complex(){}


void Complex::getType(){
	if (type == 'r'){
		convertPol();
	}
	else if (type == 'p'){
		convertRec();
	}
	else {
		cout << "invalid data type" << endl;
	}


}

void Complex::convertPol(){
	
	double x,y;
	x=a;
	y=b;
	a = sqrt(pow(x, 2)+pow(y,2));
	b = atan(y/x);
	cout << "r " << x << " " << y << endl;
	cout << "p " << a << " " << b << endl;
	type = 'p';
}

void Complex::convertRec(){
	double x,y;
	x=a;
	y=b;
	a = x*cos(y);
	b = x*sin(y);
	cout << "r " << a << " " << b << endl;
	cout << "p " << x << " " << y << endl;
	type = 'r';
}


char Complex::getType() const{
	return type;
}

double Complex::getA() const{
	return a;
}

double Complex::getB() const{
	return b;
}

void Complex::setType(char which) {
	type=which;
}

void Complex::setA(double first){
	a=first;
}

void Complex::setB(double last){
	b=last;
}
Last edited on
I am stuck on creating a global function that will take a pointer to an object of my class (Complex) and change the complex number stored in that object for its complex conjugate.


I'm not gonna show how to change the complex what( conjugate? ) lol I'm not sure my math is that solid to whatever you mean, but let's tackle the main issue.

1) Getting a pointer from an object
2) Defining a function that takes a pointer.
3) is there some other way i can store the pointers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void functionThatTakesAPointer( Complex *pComplex )
{
    pComplex->setA( 3.6 );
    pComplex->setB( 1.7 );
    // Do anything on the Complex object as you would anywhere else, just use the -> operator
}

int main()
{
    // starting from line 17
    {
        // your for...loop
        Complex newNumber(type, a, b);
        newNumber.getType();

        functionThatTakesAPointer( &newNumber );

        myNums.push_back(newNumber);
        // the rest of the code.
    }
}


If all you wanna do is change the values of your object instance, you can simply iterate over the vector container and directly do your manipulation, but if you wanna play with pointers OR you need the pointer semantics( you're sure pointer is what you really NEED ), then all you have to do is change the vector to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

int main(){

	char type;
	double a;
	double b;
	vector<Complex*> myNums;  // <----- See the new signature now!

	string line;
	ifstream myFile;
	myFile.open("numbers.txt.");
	
	while(!myFile.eof()) {

	myFile >> type >> a >> b;
	getline(myFile, line);

	Complex *newNumber = new Complex(type, a, b); // <---- see this too?
	newNumber->getType();
        functionThatTakesAPointer( newNumber ); // Assume you still use the function I defined in the previous code
	myNums.push_back( newNumber );
	
	}

	cout << endl;
        for( int i = 0; i != myNums.size(); ++i ) delete myNums[i]; // free the used memory.
	system("pause");
	return 0;
}
Alternatively, rather than use a pointer, you can pass the object to the function by reference.

I just put together a very basic example which does not use your own class, but shows actually changing an object. The objects are given an initial value in sequence from 0 to 9. Then some of them are modified and the entire vector displayed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>

class Test {
private:
    int value;
    
public:
    Test(int n) : value(n) { };
    
        
    friend void change(Test & t);
    friend void alter(Test * t);
    
    
    friend std::ostream & operator << (std::ostream & os, const Test & t)
    {
        return os << t.value;
    }
};


// access by reference
void change(Test & t)
{
    t.value *= 11;    
}


// access via a pointer
void alter(Test * t)
{
    t->value += 300;    
}


int main()
{    
    std::vector<Test> vec;       // define the vector 
    
    for (int i=0; i<10; i++)     // store some objects in the vector
        vec.push_back(Test(i));
        
        
    change (vec.at(4));          // access by reference       
    change (vec[5]);
    
    alter(&vec.at(7));           // access via a pointer
    alter(&vec[8]);
    
        
    for (auto x : vec)           // print out the contents of the vector
        std::cout << x << '\n';
        
}


Output:
0
1
2
3
44
55
6
307
308
9


44, 55 were changed by reference .
307, 308 were changed using a pointer.

In C++ references tend to be preferred.

Last edited on
Thank you very much for your help, I have managed to get my program working now..... :)
your answers have helped me understand pointers much better, I appreciate the time you guys have taken to answer my question!

Hope you have a good day ^.^
Topic archived. No new replies allowed.