Hi,

I'm very new to C++ and am trying to write a class to model affine transformations. I have written code for a 3x3 matrix and am now trying to write code for multiplying it by (x,y,z) where z=1.

My problem is I can't return the two values for the (x,y) matrix:

int IFS::eval(const int& x, const int&y)
{

    int a = (matrix[0]*x + matrix[1]*y + matrix[2]);
    int b = (matrix[3]*x + matrix[4]*y + matrix[5]);

    return (a, b);


 }

Thanks for any help!

Edited 5 Years Ago by Nick Evan: Added CODE-tags

Welcome to DaniWeb!

You could do two things.

1) Make a struct, fill the struct with the two values, and return it.
2) "Return" a and b by reference.

Also, please use code tags when posting code.

Good luck,

David

Hi,

Thanks for your help, I'm not entirely sure what you mean though. I have done this:

int IFS::eval(const int& x, const int&y)
{

    int a = (matrix[0]*x + matrix[1]*y + matrix[2]);
    int b = (matrix[3]*x + matrix[4]*y + matrix[5]);

    struct val {int a; int b};
    return (struct);

}

but it returns errors asking for ; and ). Sorry I would try fixing it for longer before asking for help again, only i've been at this for hours!

Sorry for not using tagging, hope I've done it right now.
Thanks again,
Laura

You did indeed use the tags properly :)

Method 1

struct MyStruct
{
int a,b;
}

MyStruct IFS::eval(int x, int y)
{
 int a = ...
 int b = ...
 MyStruct mystruct;
 mystruct.a = a;
 mystruct.b = b;
 return mystruct;
}

.....

Call with:

MyStruct result = IFS::eval(x,y);

Method 2:

void IFS::eval(int x, int y, int &a, int &b)
{
 a = ...
 b = ...
}

Call with:

int a, b;
IFS::eval(x,y,a,b);

I hope this helps!

David

Comments
Very helpful thank you

Hi,

Thank you, it certainly did help. However, both methods give me errors when trying to cout, which i didn't have before when i returned only 1 value. I used method 2 like you said, except i called with

int x=1;
    int y=2;
    int a, b;
    f.eval(x, y, a, b);

which was fine. But then when i write

cout<<f.eval(x, y, a, b);

i get 19 errors saying
candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|

and:
no match for 'operator<<' in 'std::cout << f.IFS::eval(x, y, ((int&)(& a)), ((int&)(& b)))'|

Have you any idea why this is?

Thank you, sorry,
Laura

The function returns void (i.e. nothing) so you can't output it. Simply do:

std::cout << a << " " << b << std::endl;

David

Hi,

I was just experimenting with using the struct method instead. But I get the error: 'MyStruct' does not name a type in these two sections of code:

public:
Mystruct eval(long double, long double);

and also in the line

MyStruct IFS::eval(long double x, long double y)

I have declared outside of my class:

struct MyStruct
{
    int a;
    int b;
};

Do you know what is going wrong?
Thanks

Edited 5 Years Ago by megaLU: n/a

You probably declared it after you tried to use it. Can you send us a compilable example? Also, watch out for case sensitivity - 'Mystruct' vs 'MyStruct'.

Hmm, i'm not sure how i'd do that without posting my whole code (200 lines) so i will post the basic structure, with details missing.

#include <iostream>
#include <cmath>
#include <vector>
#include <stdio.h>
#include <stdlib.h> 

using namespace std; 
struct MyStruct
{    
long double a;    
long double b;
}; 
class example
{ public:  .....         
           Mystruct eval(long double, long double);         
           .....
private:   ..... 
};
/* operators */

int main ()
{
.........    
long double x= 0.0;    
long double y= 0.0;    
MyStruct result = example::eval(x,y);
}

/* constructors */ 

.......
Mystruct example::eval(long double x, long double y)
{     
int a = (matrix[0]*x + matrix[1]*y + matrix[2]);    
int b = (matrix[3]*x + matrix[4]*y + matrix[5]);    
MyStruct mystruct;    
mystruct.a = a;    
mystruct.b = b;    
return mystruct;
}

With an additional error of 'eval' is not a member of 'example' when I call it.

Sorry to ask another question on this thread. Thanks for any help.

Edited 5 Years Ago by megaLU: n/a

This question has already been answered. Start a new discussion instead.