Computer Science Canada Splitting my program into multiple files (wrong argument type) |
Author: | fletch_to_99 [ Thu Apr 19, 2012 12:13 pm ] | ||||||
Post subject: | Splitting my program into multiple files (wrong argument type) | ||||||
What is it you are trying to achieve? Trying to break my program into multiple files. What is the problem you are having? Argument is wrong type once in a seprate file however when all in one file the program runs great Describe what you have tried to solve this problem I'm not sure what to do. My teacher didn't know what was wrong either however he stated I have the wrong desgin for my code (not using it as Object Oriented but more like procedural). This is my first attempt at having multiple files for the program, any tips would help! ![]() Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) See code below Matt L C10Q6 Main.t
Matt L C10Q6 Types.t
Matt L C10Q6 Calculations.t
Please specify what version of Turing you are using Latest (4.1.1?) |
Author: | Tony [ Thu Apr 19, 2012 1:17 pm ] |
Post subject: | Re: Splitting my program into multiple files (wrong argument type) |
fletch_to_99 @ Thu Apr 19, 2012 12:13 pm wrote: Argument is wrong type once in a seprate file however when all in one file the program runs great Which function / line is the problem on? What is the expected type? What is the provided type? |
Author: | fletch_to_99 [ Thu Apr 19, 2012 2:58 pm ] |
Post subject: | RE:Splitting my program into multiple files (wrong argument type) |
It's line 53 in the main file. Its calling the calcAvg function in the Calculations class which is expecting: students : array 1 .. * of Assignment. This is whats provided: var students : array 1 .. num of Assignment. |
Author: | DemonWasp [ Thu Apr 19, 2012 3:26 pm ] | ||
Post subject: | RE:Splitting my program into multiple files (wrong argument type) | ||
You're running into a fundamental design flaw in Turing. Basically, include doesn't work like you're hoping it does. What include "foo.t" actually does: - open "foo.t" and grab its contents - put those contents in place of the include statement in the original file. What's happening in your program: - Main.t includes Types.t (awesome, now Assignment is defined) - Main.t includes Calculations.t - Calculations.t includes Types.t (now Assignment is defined twice, in different "scopes") The problem results because the Assignment type is defined twice, and the types are not considered to be the same. Competent languages are designed to avoid this problem (by providing the programmer the ability to solve the problem, or by solving the problem inherently in their design). C and C++ take the former route, with #ifdef and #define, while Java and most other high-level languages take the latter route. In Turing, the best I've been able to do is to make sure that each file is included exactly once. You probably want: - Calculations.t includes Types.t - Main.t includes Calculations.t (but NOT Types.t). It's possible to re-create this in one file, to help you understand and play with it:
|
Author: | fletch_to_99 [ Thu Apr 19, 2012 6:35 pm ] | ||
Post subject: | Re: RE:Splitting my program into multiple files (wrong argument type) | ||
DemonWasp @ Thu Apr 19, 2012 3:26 pm wrote: You're running into a fundamental design flaw in Turing. Basically, include doesn't work like you're hoping it does.
What include "foo.t" actually does: - open "foo.t" and grab its contents - put those contents in place of the include statement in the original file. What's happening in your program: - Main.t includes Types.t (awesome, now Assignment is defined) - Main.t includes Calculations.t - Calculations.t includes Types.t (now Assignment is defined twice, in different "scopes") The problem results because the Assignment type is defined twice, and the types are not considered to be the same. Competent languages are designed to avoid this problem (by providing the programmer the ability to solve the problem, or by solving the problem inherently in their design). C and C++ take the former route, with #ifdef and #define, while Java and most other high-level languages take the latter route. In Turing, the best I've been able to do is to make sure that each file is included exactly once. You probably want: - Calculations.t includes Types.t - Main.t includes Calculations.t (but NOT Types.t). It's possible to re-create this in one file, to help you understand and play with it:
Thanks for the reply! I will try this out at school tomorrow. I use java at home to make plugins for minecraft and in Java for each class in a separate file you have to import everything again, that's what stumped me in turing. Again, thanks!! |
Author: | Dreadnought [ Fri Apr 20, 2012 5:46 pm ] | ||||
Post subject: | Re: Splitting my program into multiple files (wrong argument type) | ||||
fletch_to_99 wrote: I use java at home to make plugins for minecraft and in Java for each class in a separate file you have to import everything again, that's what stumped me in turing.
Turing has import which is closer to the equivalent in java (it works with export and lets you use things like module, class and unit to organize code). The include statement just opens the Turing file it is given and copies the code in the place of the include statement. It does not care about code organization. For example you can even do this (which looks kinda dumb):
Notice that I split a for loop, and use a for loop variable in the second file that is only declared in the first file. Anyway, all this to say that import may have been what you wanted to use. |