Help with a class, again....

I have this class:

class Horario {
horpiece p [336];
public:
void asignarl (horpiece p2 [336]){

p=p2;

}
horpiece obtener (int id);
} hor;


Asignar means set, or assign. When I try to build this it says horpiece* is not equivalent to horpiece [336]

So I just want the class Horario to have an array of horpieces called p
I want to set p=p2
So that I create an array of horpieces and they are set as p for my horario object.

But for some reason p2 and p are not compatible, I even changed "p=p2;" to p=*p2, *p=p2, p=&p2, &p=p2....

Basically I have no Idea of how classes and pointers work yet.

Any help?
When you define a formal parameter as type name[size] what you're really passing is just a pointer. All these functions are identical:
 
void asignarl (horpiece p2[336]){
 
void asignarl (horpiece p2[61681]){
 
void asignarl (horpiece p2[]){
 
void asignarl (horpiece *p2){
There's no way to pass arrays to a function, but even if there was it wouldn't matter, since you can't assign arrays to each other.
1
2
3
int x[4] = {0};
int y[4];
y = x; //Error. 

You can use std::copy():
1
2
3
void asignarl (horpiece p2[336]){
    std::copy(p2, p2 + 336, this->p);
}


PS: You can also use std::array:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <array>

class Horario{
public:
    typename std::array<horpiece, 336> p_type;
private:
    p_type p;
public:
    void asignarl (const p_type &p2){
        p=p2;
    }
    horpiece obtener (int id);
} hor;
Last edited on
Allright, thank you in advance, I will first try std::copy
It.....WORKS!! Perfect, let's now try the program and then continue. Thanks! :D
Topic archived. No new replies allowed.