LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Valac: Compiling into C code: Expected semicolon in the wrong place (https://www.linuxquestions.org/questions/programming-9/valac-compiling-into-c-code-expected-semicolon-in-the-wrong-place-839440/)

Kenny_Strawn 10-20-2010 09:58 PM

Valac: Compiling into C code: Expected semicolon in the wrong place
 
Here is my problem: I have a Vala program that includes classes with nested classes. When I try to initialize classes (using the variable type and the variable, followed by the class's arguments [three unsigned 8-bit integers] in parentheses) and try to compile the Vala code to C code (thanks Steshenko, that's what I tried to AVOID doing, as I really wanted to create a shared object file, but now I know I have to use C code as a middleman), the compiler tells me it expects a semicolon between the class variable name and its members.

What?! A semicolon in a bad place, that's for sure! Why would I want to put a semicolon *BETWEEN* the class initialization and its members? Isn't the whole point of a class to *HAVE* different members?

Here is my class:

Code:

public class Gtk.rgba : GLib.Object {
    public class fg_color : GLib.Object {
        private uint8 red;
        private uint8 green;
        private uint8 blue;
               
        //Constructor
        public fg_color(uint8 r, uint8 g, uint8 b) {
            r = red;
            g = green;
            b = blue;
                       
            uint full_color = red * green * blue;
        }
    }
       
    public class bg_color : GLib.Object {
        private uint8 red;
        private uint8 green;
        private uint8 blue;
               
        //Constructor
        public bg_color(uint8 r, uint8 g, uint8 b) {
            r = red;
            g = green;
            b = blue;
                       
            uint full_color = red * green * blue;
        }
    }
       
    //Toplevel constructor
    public rgba(var bg, var fg) {
        bg = bg_color;
        fg = fg_color;
    }
       
    public static void init(double alpha) {
        double alpha_t = alpha;

        //Here's where the error is
        for (alpha_t > 0; alpha_t < 1; ++alpha_t) {
            fg_color fg_increment(red++, green++, blue++);
            bg_color bg_decrement(red--, green--, blue--);
                                ^ //The compiler is telling me to put a semicolon *HERE* <yawn>.
        }
    }
}


Aquarius_Girl 10-21-2010 12:39 AM

Shouldn't there be a semicolon at the end of the nested classes too ?

Kenny_Strawn 10-21-2010 01:01 AM

Yeah, inserting semicolons at the end of the nested classes causes more errors. This is Vala we're talking about here, the programming language written for the GNOME project that uses GLib as the standard library, has a syntax very much like Java's and C#'s, and compiles to C. You must have mistaken it for Java; that happens often.

Kenny_Strawn 10-21-2010 01:05 AM

Besides, Anisha: Here's the whole point of this post.

Notice the comments in my code pointing you to the location of the "error"? Scroll to the bottom of the code block, and you'll find them, as well as an upward-pointing carat pointing you to the location of just WHERE the compiler is telling me to insert the semicolon (Hint: It's in an odd location).

Aquarius_Girl 10-21-2010 01:21 AM

Well yes I mistook it for C++ :doh: and I am sorry for taking your thread off the zero reply list !

I saw your bolded code text earlier and the reason I pointed to the class semicolons was that compiler often points out to the wrong place w.r.t semicolons in C/C++ !

Check whether the functions are supposed to be called the way you have called them ?
I know you must have already seen this page:
http://www.linux.com/archive/feature/154784

and you can report my post for deletion in order to keep your thread in the zero reply list !

graemef 10-21-2010 01:23 AM

Sometimes errors propagate through to give unusual error messages.

So do you need to have semi-colons to end the nested classes? If so put them there and then check and fix the new error messages that appear.

Completely unrelated to your compiler errors but I don't think that the following does what you want it to do.
Code:

            r = red;
            g = green;
            b = blue;


graemef 10-21-2010 01:37 AM

Another point I don't think that your outer class knows about the identifiers red, green or blue.

Again unrelated to the compiler errors but you will probably have a hard time distinguishing the difference between the fg_color and bg_color, they are almost identical.

Kenny_Strawn 10-21-2010 01:57 AM

The following functions are constructors:

Code:

...
public fg_color(uint8 r, uint8 g, uint8 b) {
    r = red;
    g = green;
    b = blue;
    uint8 full_color = red * green * blue;
}
...
public bg_color(uint8 r, uint8 g, uint8 b) {
    r = red;
    g = green;
    b = blue;

    uint full_color = red * green * blue;
}

They are for one purpose: class construction, so you can use private numerical values (the 'red', 'green', and 'blue' values) publicly: when initializing the class, you do it like a function:

Code:

Gtk.rgba.bg_color foo(17, 17, 17);
Gtk.rgba.fg_color bar(241, 93, 44);

Then, you use the 'Gtk.rgba.init()' function to initialize the alpha value. Now let's try this again:

Code:

Gtk.rgba.bg_color foo(17, 17, 17);
Gtk.rgba.fg_color bar(241, 93, 44);
Gtk.rgba.init(0.5);

And besides: If GTK+ itself has values with the same names as these in a C struct (located in gdk/gdkcolor.h), why won't this do the same thing?

Kenny_Strawn 10-21-2010 01:58 AM

Quote:

Originally Posted by graemef (Post 4134410)
Again unrelated to the compiler errors but you will probably have a hard time distinguishing the difference between the fg_color and bg_color, they are almost identical.

One is for the foreground and one is for the background; other than that they *ARE* identical.

Kenny_Strawn 10-21-2010 02:05 AM

These are the errors I get when I put semicolons at the end of the nested classes:

Code:

kenny_strawn@kenny-AOA110:~/Documents$ valac --pkg=gtk+-2.0 gtk_rgba.vala -H gtk_rgba.h
gtk_rgba.vala:18.6-18.6: error: syntax error, statements outside blocks allowed only in root namespace
          };
            ^
gtk_rgba.vala:33.3-33.3: error: syntax error, statements outside blocks allowed only in root namespace
        };
        ^
gtk_rgba.vala:44.25-44.25: error: syntax error, expected `;'
                        fg_color fg_increment(red++, green++, blue++);
                                            ^
Compilation failed: 3 error(s), 0 warning(s)
kenny_strawn@kenny-AOA110:~/Documents$

The last error is what I already had before the semicolons at the end of the classes.

Aquarius_Girl 10-21-2010 02:11 AM

Code:

//Here's where the error is
        for (alpha_t > 0; alpha_t < 1; ++alpha_t) {
            fg_color fg_increment(red++, green++, blue++);
            bg_color bg_decrement(red--, green--, blue--);
                                ^ //The compiler is telling me to put a semicolon *HERE* <yawn>.
        }

Where are the bolded functions defined in your code ? As I can see you have called them here. To which class do they belong ?

Kenny_Strawn 10-21-2010 02:59 AM

Those are initializations of the bg_color and fg_color classes, as declared before the functions (in turn within a function definition):

Code:

...
public static void init(double alpha) {
    int alpha_t = alpha;
    for(alpha_t > 0; alpha_t < 1; ++alpha_t) {
        fg_color fg_increment(red++, green++, blue++);
        bg_color bg_decrement(red--, green--, blue--);
    }
}

And what this function is supposed to do is this:

While the alpha value is between 0 and 1 and increments, the foreground color is supposed to increment and the background color decrement. This is literally what drives alpha blending: the background (at a lower alpha value) is supposed to blend into the foreground. This is how you get the look of partially transparent colors.

graemef 10-21-2010 02:59 AM

anishakaul My interpretation is that the bold values are object of type fg_color and bg_color respectively.

Kenny_Straw As I read the code red, green and blue are properties of the inner classes and not visible at the outer class level. That is what the compiler is complaining about and importantly this occurs on the line prior to the one you had indicated. Which means that the compiler message does make some sense.

graemef 10-21-2010 03:02 AM

I'd like to emphasis the point I gave earlier.
Code:

            r = red;
            g = green;
            b = blue;

The above is wrong. Your assignments are the wrong way around.

Aquarius_Girl 10-21-2010 03:28 AM

I had missed post 8 *sigh*

Oh yes, it should be the other way round, now I do notice it too..

red = r;
green = g;
blue = b;

Quote:

Originally Posted by graemef (Post 4134410)
Another point I don't think that your outer class knows about the identifiers red, green or blue.

Yes, C++ doesn't allow that too ! Vala must not be too different.
http://publib.boulder.ibm.com/infoce...%2Fcplr061.htm


All times are GMT -5. The time now is 07:05 PM.