Quick Class Parameter - How?

I wasn't sure how to describe this situation so I don't know what to search for on the web.

I have two classes, A and B. One of the constructors for B takes a class A as a parameter. This works fine, but it is annoying because I have to create a class A object and pass it as a parameter.
1
2
A MyTemp (2, 14);
B MyObject (Blah, MyTemp);


Is there a way I can make class A on the fly and hand it to class B like this?
B MyObject (Blah, A(2, 14));

Obviously, that doesn't work. It gives the error "function-style cast".
But, I hope you see the point about making the class on the fly. I was thinking that maybe a function would do but I think that that would be a bit redundant if there is another solution.

Thanks,
-LB
That should work. The following code should compile nicely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A{
    int i;
public:
    A(int I) : i(I){}
};

class B{
    int i;
    A a;
public:
    B(int I, A alpha) : i(I), a(alpha){}
};

int main(){
    B beta(5, A(7));
    return 0;
}


The error is somewhere else. Post more code.
Hmm, the error must be actually the code I lack. I don't know what the ": i(I)" or " A(int I) : i(I)" code is, I haven't seen it before. Could you explain what it is?
Last edited on
These are irrelevant. They are initialiser lists. This is a way to call the constructors of members. This could be a little bit quicker, since otherwise default constructors are called. It makes no difference for primitive data types though.

1
2
A(int I) : i(I){
}
is eqivalent to
1
2
3
A(int I){
    i = I;
}

However if you want to do this with B's constructor, you'll have to write a default constructor for A.
1
2
3
4
B(int I, A alpha){//the default constructor of A will be called here.
    i = I;
    a = alpha;
}
Huh, that is exactly what I am doing but I am getting that error.

For the sake of not stretching this page to smithereens, I have posted the code over here:
http://paste.pocoo.org/show/253141/

I can define an image OK like this:
Image MyPicture;

But when I try to do this, which you say compiles fine, I get the error.
Image MyPicture (Vect(100, 100));
This code SHOULD create an image with a size 100 by 100.

Also, I am still learning and I will change and optimize my classes, I know there is a lot of stuff I could be doing better.


This also leads to my other question; you will notice that in a couple of the classes one of the constructors uses a bool DUMMY parameter. I did this because the compiler gives me an error if the first parameter is the same type as the class itself. It works as the second paremeter. Why won't it let me use it as the first parameter?
I compiled your header with this main:
1
2
3
4
int main(){
    Image i(Vect(5, 3));
    return 0;
}


I had no problems with that (Except that you should add using namespace std; or change each vector<...> to std::vector<...>).

A constructor that takes object of the same class is a special case - the copy constructor. It's Myclass(Myclass&) or Myclass(const Myclass&). I suppose Myclass(Myclass) would conflict with the copy constructor and therefore is not allowed.
Last edited on
Thanks for pointing out the using namespace std; bit, I actually had that above the include for my header.

Anyway, I really don't see why it just does not let me compile that! I'm using Visual C++ 2008, if that makes any difference. I even tried in a new, clean project and it still gives the same three errors every time:
error C2059: syntax error : 'function-style cast'
error C2059: syntax error : ')'
error C2143: syntax error : missing ')' before ';'
Last edited on
It's Myclass(Myclass&) or Myclass(const Myclass&).


It actually should only be Myclass(const Myclass&);. The other one isn't a copy constructor.
It actually should only be Myclass(const Myclass&);. The other one isn't a copy constructor.


When I do that, then it gives me errors when I try to access functions from Myclass. Accessing variables works OK, though?
Last edited on
Likely because your class isn't const correct.

Functions that do not change the state of the class (like get() functions) should be declared as const functions.

For example in your pixel_t class:

1
2
3
4
5
6
//  unsigned long GetRGB(){ return(R + (G * 256) + (B * 65536)); }  // instead of this..
//  do this:
unsigned long GetRGB() const  // <- note the 'const'
{
  return R + (G*256) + (B*65536);
}
Thanks, I forgot that. That fixes those errors, but I am still getting the error that has been the center of this topic;
error C2059: syntax error : 'function-style cast'
error C2059: syntax error : ')'
error C2143: syntax error : missing ')' before ';'


And again I am using Visual C++ 2008 Express Edition.

If you all are compiling this without a problem, then what's my issue?
I jumped in this thread a little late. Which line of code is producing that error?
I can do this without errors:
Image MyPicture;
or
1
2
Vect Temp (100, 100);
Image MyPicture (Temp);


But what I want to do is this:
Image MyPicture (Vect(100, 100));
However it produces the error.
Last edited on
Here is the updated code:
http://paste.pocoo.org/show/253272/
That is strange, I don't know why you'd get that error. It is working just fine here, as it should.
I'm so sorry everyone! It seems that this code Image MyImage (Vect(100, 100)); can't be done inside a class, struct, or union. When I move the code outside of it, it compiles without a problem. I probably should have mentioned where I was declaring it...
Topic archived. No new replies allowed.