0

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 by Nick Evan: Added CODE-tags

2
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by megaLU
Featured Replies
  • You did indeed use the tags properly :) Method 1 [code] 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); [/code] … Read More

  • The function returns void (i.e. nothing) so you can't output it. Simply do: [code] std::cout << a << " " << b << std::endl; [/code] David Read More

0

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

0

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

1

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
0

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

1

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

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

David

0

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 by megaLU: n/a

0

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'.

0

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 by megaLU: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.