I'm having trouble with passing a pointer to a function that is called by another pointer. I'm trying to modify a pointer (i.e. p1) that calls the function min (i.e. p1->min()) which takes a pointer as a parameter (i.e. p1->min(*p2)). Note: *p2 isn't modified at all, just passed for its values, for which p1 will be modified based on the values of p2.
Note: I removed irrelevant code (only inside functions, everything else is as is) to make it easier to read.
Test.h
// Don't worry about the create method, just assume it works
// I'm having issues with the "min" function, more details below
class Test {
protected:
std::vector<std::vector<std::string> > vec;
public:
static Test *create(std::string file); // instantiates vec
void *min(Test *); // modifies vec
};
Test.cc
// Don't worry about create (factory method), just assume it works
// "min" is causing compiler errors (see below)
Test *Test::create(string file) { /* instantiates vec with file contents */ }
void *Test::min(const Test *&p) { /* modifies vec */ }
main.cc
// Main cannot change, this is how it must be implemented
// However, Test.cc and Test.h CAN be modified.
Test *p1 = Test::create("file1");
Test *p2 = Test::create("file2");
p1->min(*p2); // modify p1 based on values of p2, p2 is NOT modified
Compiler Errors:
fatal error: control reaches end of non-void function
What is weird is that its declared void but expecting a return value So, when I do return something it shows up another compiler error
fatal error: no viable conversion from return value of type 'Test' to function return type 'void *'
I'm so confused on the compile errors. I'm thinking it has something to do with my declarations. Note: There is no constructor since the base class must use a factory method while the derived classes use their own constructors, hence the *Test::create and *Test::min.
Please help.
Test::min's declaration doesn't match it's definition. The arguments are different. And are you sure you wantminto returnvoid*? What's the significance of the return value?void*is not the same asvoid. Voting to close as typo