error: cannot convert 'CPacket' to 'CPacket*'

Hi, I've just joined the forum because I've found the tutorials on this website really useful and I've kinda come into a problem with a program I've made in C++ and am hoping for some help.

The title is the error message I get from the compiler.
I've put some of the code below.

Its meant to be used to generate data for a Network Simulator and all i need to do is ask a user to input data and then ask if they want to store to a file. But obviously this data needs to be validated so this is where the problem occurs....

At the beginning of the program I made an array of the class CPacket which is the end result of where I want to store my data. I also have another class called temp_CPacket which stores the values in temporarily allowing me to validate them. Since this is a temp storage place, I only initialize the 1 class.

The variable nPackets is a global variable which I use to keep track of which "number" CPacket I'm in within the array. I made it global so that it can be tracked throughout the entire program. I had made this a Class variable before hand but then I ran into this problem and I though that making it a global variable would fix it....it didn't, and I just left it as a global because it was easier really.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    int selection=NULL, /*nPackets=1,*/ *ptr_nPackets; 
//stores the users choice in the menu
    nPackets--; 
//gets rid of the initial packet created due to initialisation
    ptr_nPackets = &nPackets;
    char memoryFlush = NULL, another_packet=NULL; 
//stores whether the packets have been removed from memory (either by file write or discard)
    string dat, buffer; 
//stores the data value temporarily and stores the input from user for the data packets
    CPacket packet[packet_max];
    temp_CPacket temp_packet;

    while(true)
    {



The problem occurs when I try to pass forward the array of class CPacket into the function create_packet( ) which I need to put the global varible nPacket (the ptr_nPackets was another attempt to get around this problem but again this failed) but then I get the error code in the title....

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
78
79
cout << "Do you wish to create another packet? (Y/N)\n: ";
                    cin >> another_packet;
                    while (true)
                    {
                        if(make_another_packet(another_packet)==1)
                        {
                            break;
                        }
                        else
                        {
                            create_packet(temp_packet, packet[nPackets] /*ptr_nPackets*/ ); //error
                            break;                            
                        }
                    }

//........................ //skip to create_packet, I presume the error is at the bottom of this

void create_packet(temp_CPacket temp, CPacket packet[] /*int* ptr_nPackets*/ )
{
    cout << "\nSource Address (1-1024): ";
    cin.ignore(255,'\n');
    getline(cin, temp.buffer);

    temp.src = validate_string(temp.buffer, temp.src);
    while((temp.src > 1024 || temp.src < 1))
    {
        cout << "\nInvalid data entry, please re-enter: ";
        getline(cin, temp.buffer);
        temp.src = validate_string(temp.buffer, temp.src);
    }

    cout << "\nDestination Address (1-1024): ";
    getline(cin, temp.buffer);
    cin.ignore(255,'\n');
    temp.dst = validate_string(temp.buffer, temp.dst);
    while((temp.dst > 1024 || temp.dst < 1))
    {
        cout << "\nInvalid data entry, please re-enter: ";
        getline(cin, temp.buffer);
        temp.dst = validate_string(temp.buffer, temp.dst);
    }

    cout << "\nType (0-10): ";
    getline(cin, temp.buffer);
    cin.ignore(255,'\n');
    temp.typ = validate_string(temp.buffer, temp.typ);
    while((temp.typ > 10 || temp.typ < 0))
    {
        cout << "\nInvalid data entry, please re-enter: ";
        getline(cin, temp.buffer);
        temp.typ = validate_string(temp.buffer, temp.typ);
    }

    cout << "\nPort (1-1024): ";
    getline(cin, temp.buffer);
    cin.ignore(255,'\n');
    temp.prt = validate_string(temp.buffer, temp.prt);
    while((temp.prt > 1024 || temp.prt < 1))
    {
        cout << "\nInvalid data entry, please re-enter: ";
        getline(cin, temp.buffer);
        temp.prt = validate_string(temp.buffer, temp.prt);
    }

    cout << "\nData (50 chars max): ";
    getline(cin, temp.dat);
    while (validate_dataString(temp.dat)==false)
    {
        cout << "\nInvalid data entry, please re-enter: ";
        getline(cin, temp.dat);
        cin.ignore(255,'\n');
    }

    cout << "\nPacket " << nPackets << " created.";
    nPackets++;
    packet[nPackets].set_values(temp); //possibly the location of error?

}


So that's my problem, sorry if I'm not making much sense, I have no idea what the hell is happening with it and its really frustrating me because I think its actually quite a simple problem to do with the relationship between arrays and pointers but I have no idea what.... If you want to see the full code, just say and I'll post it.

Thanks if you can help!
Last edited on
The error message is very clear. So I do not see any problem. Your function create_packet

void create_packet(temp_CPacket temp, CPacket packet[] /*int* ptr_nPackets*/ )

is defined such a way that its second parameter is a pointer to CPacket, that is CPacket *. But you are trying to pass it object of the type CPacket

create_packet(temp_packet, packet[nPackets] /*ptr_nPackets*/ );

because packet[nPackets] denotes the alement of the array that corresponds to the index nPackets

Oh right! That's make sense. Well how would i implement it how i wanted to? Because i cant imagine how to get it to do it any other way other than how I did it....probably why i did like that I suppose...

Also, thanks for the reply, gives me a better insight as to why, I was really confused as to why it was telling me that my second parameter was a pointer to CPacket, i presume its to do with the fact that it's an array?


EDIT:
I understand that its telling me its a pointer to CPacket, this is I'm assuming to do with the fact that arrays are just pointers to the beginning of a list of memory locations. But I just dont understand what I could do to get around this problem....
It's kinda the only thing that's stopping me from finishing this program as well, granted my code writing is a mess but I can look over that without stressing too much.
Last edited on
create_packet(temp_packet, packet[nPackets] /*ptr_nPackets*/ ); //error
This is the code version of vlad's exact words.
create_packet(temp_packet, packet /*ptr_nPackets*/ ); //error
Try this:

 
create_packet(temp_packet, packet + nPackets);


If packet is the pointer to the beginning of your array, packet + nPackets should be the pointer to the beginning + nPackets.
Last edited on
Yes! That's made it work! Thank you so much for the help! I can't believe it was something so simple to get it this to finally work!

Got it all up and running now! ..... and then I realise I need to remove all my ptr_nPackets and stuff. Oh well, that's minor issues compared to the create_packet one. Once again, thank you everybody for helping!
Topic archived. No new replies allowed.