How to pass variable to function as reference, variable and as pointer?

I am currently working on a project, in which i need to pass object to a function. Based on this http://www.cplusplus.com/articles/z6vU7k9E/
I understood that what I am interested in is to pass my object as const pointer.


But for some reason am I keep failing at accomplishing this..

spectogram.h
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
#pragma once
 
#include <sigpack.h>
#include <../record/AudioFile.h>
#include <iostream>
 
typedef float SAMPLE;
 
 
class spectogram {
public:
    spectogram();
    arma::mat generate_spectogram(const AudioFile<SAMPLE> *file)
    {
        if(file->getNumChannels() == 2)
        {
            arma::Col<SAMPLE> ch1 = file->samples[0];
            arma::Col<SAMPLE> ch2 = file->samples[1];
        }
        else
        {
            arma::Col<SAMPLE> ch1 = file->samples[0];
        }
        //return sp::specgram (file->samples[0]);
    }
};




record.h
1
2
3
4
5
6
7
8
9
10
11

#include <AudioFile.h>
#include <iostream>
 
typedef float SAMPLE;
class record {
public:
    record();
    void start_record();
    AudioFile<SAMPLE>           file;
};


main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <database.h>
#include <match.h>
#include <record.h>
#include <spectogram.h>
 
#include <iostream>
 
int main()
{
    record  somethis;
    somethis.start_record();
    spectogram specto;
    specto.generate_spectogram(&somethis.file);
    return 0;
}


I am trying to pass the variable somethis.file as a pointer to the function specto.generate_spectogram() to the function, but for some reason is the way I am doing this is incorrect.


What am I doing wrong?
Last edited on
I don't see anything that is obviously wrong here. Personally I would have passed by reference instead of a pointer but it should work both ways. What error message do you get? If getNumChannels() has not been marked as const you would get an error on line 15 in spectogram.h because you can't call a non-const member function though a "const pointer".
Last edited on
I am getting a segmentation fault in the else statement, i can't can't copy my pointer vector to the internal vector, making me suspisous whether it is pointing t o the right thing..?
One problem that I didn't see before is that you do not return anything from the function despite the return type being non-void.

If file->samples is an empty vector, or an invalid pointer, that would also be a problem.
file->samples should be a 2d vector. It is not empty...

And it seems that the missing return caused the segmentation fault... god dammit.. thanks for the help.
Topic archived. No new replies allowed.