1

I don't get how to use structs properly to achieve my goal of calculating Fractions (it is required). Quite frankly I don't have much of an idea of what I'm doing, this is only my 3rd class in C++ and I feel lost...this was the task assigned to us

Your enter() function accepts a fraction from the user. Your simplify() function simplifies the fraction that it receives, if possible. Your display() function displays the fraction that it receives.

Your global functions use a Fraction type. A Fraction type holds the numerator and denominator of a fraction as separate data members.

This is my program, only the main EXCEPT the "cin" and "cout" and the GCF function was provided by the professor, all other functions and struct outside of the main i tried to do myself...

 #include <iostream>
 using namespace std;

void entry (int a, int b);
void simplify (double c);
void display(int x, int y)

int main() 
{

    struct Fraction fraction;
         cout << "Enter a numerator: " << endl;
         cin >> fraction.num;
         cout << "Enter a denominator: " << endl;
         cin >> fraction.den;

     cout << "Fraction Simplifier" << endl;
     cout << "===================" << endl;

     enter(&fraction);
     simplify(&fraction);
     display(fraction);
 }



        struct Fraction {
                 int num;
                 int den;
                }


        struct Fraction fraction{
                 fraction.num;
                 fraction.den;
                }

        void display(int num, int den) {
                 cout << fraction.num << endl;
                 cout << fraction.den << endl;
                }



// Great Common Factor (Euclid's Algorithm), provided by Professor

int gcf( int num1, int num2 )

{

     int remainder = num2 % num1;
     if ( remainder != 0 )
      {
         return gcf( remainder,num1 );
       }
     return num1;
}

these are my errors:

w2.cpp: In function 'int main()':   
w2.cpp: 14:  error: aggregate 'Fraction fraction' has incomplete type and cannot be defined 
w2.cpp: 23:  error: 'enter' was not declared in this scope   
w2.cpp: At global scope: w2.cpp:35: error: function definition does not declare parameters  
w2.cpp: In function 'void display(int, int)':   
w2.cpp: 41:  error: 'fraction' was not declared in this scope

I'm sorry for the really long post, but any and all help is greatly appreciated. AND if someone could point me to a helpful C++ book that I could read while at home and or in lectures (because of a language barrier i cannot understand my prof well enough) would also be appreciated

Mat
  • 202,337
  • 40
  • 393
  • 406
SorryEh
  • 900
  • 2
  • 15
  • 47
  • 3
    [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/636019) – ildjarn May 30 '12 at 03:19
  • sweet, thank you for the list of books! – SorryEh May 30 '12 at 03:25
  • 2
    In C++ you have to put the other functions before than the function where they are used. Type the other functions before main. Also you can use forward declaration but at this point, just writing the functions before than the function where they are called is probably ok. Also you are missing a ; in line 6 – Topo May 30 '12 at 03:26

2 Answers2

3

Let's walk through these:

error: aggregate 'Fraction fraction' has incomplete type and cannot be defined

Now, in main(), you said struct Fraction fraction;. At this point, you're forward-declaring your struct. It is not complete, so you can't use it as if it were.

You should have your whole Fraction struct defined before main(). Also note that the struct in struct Fraction fraction; is unnecessary, and left over from C.

 error: 'enter' was not declared in this scope

Simple. You've declared entry() up top, but you're trying to use enter(). Not much more to be said.

 At global scope: w2.cpp:35: error: function definition does not declare parameters

Now this is a bit more confusing. This is the offending line:

struct Fraction fraction{

How the compiler sees this is that it is a function returning a Fraction, but it's missing its parameter list. I'm not exactly sure what you're trying to do with this block of code.

error: 'fraction' was not declared in this scope

Looks like you're trying to use an object declared somewhere else. If you want the one from main(), you'll have to pass it in as an argument. If you want a global variable fraction, all you need in the global space is:

Fraction fraction;

This should occur after the Fraction struct. Also note that because this has the same object name as the one in main(), the one in main() shadows this one, and if you want to access the global one from main() you need to use ::fraction.

I hope that helps clear up some of the understanding.

Some other errors I see are:

enter(&fraction);

You're passing a Fraction * to a function that takes two ints. I think you'd want this one to take a Fraction &. Then you can just call it like enter (fraction); to have it modify the object passed in.

simplify(&fraction);

Similar, but this one takes a double. I think you'd want it to take a Fraction & as well.

  • Your entry and simplify functions never get defined, but you still try to use them.
  • display should take a Fraction in order to print the parts of it.
chris
  • 60,560
  • 13
  • 143
  • 205
  • wow this and dirkgently made things so much more clear. I've gotten closer to finishing it now most of the errors are gone and I just need to fix the other functions. phew i was stressing over this haha. One question, is it alright if i print this out n make some copies for some classmates they could use this info because i know some are in the same boat as me. – SorryEh May 30 '12 at 04:23
3

A list of recommended books on C++. Searching this site helps too.

In C++, structures (or classes) and unions form the two basic types of user defined data structure. A user defined data structure is a model/blue-print of something (it could be a real-world quantity or an abstract concept) you want your program to work with. So, if you wanted a structure to store your friends' names, you'd probably do something like this:

struct FriendName {
    std::string first, last;
}; // the semi-colon is required here

first and last are the members of your struct. std::string is the type of these members which tells the compiler what sort of data you want to store -- the data here being strings we use the appropriate type defined by the library.

Once you've defined something called a FriendName you can use it to store data and also work with this data. However, if you try to define FriendName again, the compiler will complain. Which is what is happening with your code.

Now, in order to use this data structure, you will need to create an object (which is a region of memory that represents a particular FriendName instance). You can create an object as follows:

FriendName fred; // note that I don't need to use struct FriendName 

and you can go ahead and use it as:

fred.first = "Fred"; // write 'fred' object's first name
fred.last = "Flintstone";

The object name works as a marker which when combined with with the . operator and a member name allows you to read/write that particular member.

Assume you wanted to read in the names from the console: In that case you'd do:

FriendName wilma;
std::cin >> wilma.first >> wilma.last; // read in 'wilma' objects members one by one

Now, there's enough up there to get you started!

Community
  • 1
  • 1
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • Good explanation. There are a good number of errors unrelated to just data structures too though, unfortunately. – chris May 30 '12 at 03:36
  • @chris: Thanks! I noticed them. But I decided going into these just as soon given the OP's relative unfamiliarity with basic C++. Also, this is homework, I don't want to spoil the party for him either :) – dirkgently May 30 '12 at 03:39
  • True, but `Quite frankly I don't have much of an idea of what I'm doing` prompted me to go through and try to explain as to get rid of the uncertainty and come out knowing why none of these worked, and how to use them properly. Some of these can be quite confusing if you don't know what you're doing and get onto a path that ends up turning out bad later. I think having these errors explained early helps to start off well rather than just fixing the code without knowing why it really worked (which would cause a huge problem later). – chris May 30 '12 at 03:43
  • 1
    @chris: I hope the OP reads both. Your answer complements mine (way more technical)! Please don't think I'm being critical. OTOH, I was hoping that someone else (read less lazy) would actually work through the individual errors :P – dirkgently May 30 '12 at 03:46
  • Don't worry about that. I know what you mean completely. Programmers unite! – chris May 30 '12 at 03:55
  • @drikgently this was excellent, I'd like to print this out to if it's okay with you, along with Chris' post to add to the explanation that I'd like to help my fellow classmates with, since they are struggling as much as I am. Thanks Dirkgently – SorryEh May 30 '12 at 04:25
  • 2
    @Umeed, it isn't copyrighted or anything. SO is meant for people to keep using even after the problem is solved. Sometime later, someone might search an identical error and come up with this. – chris May 30 '12 at 04:27
  • 1
    @Umeed: Feel free to use the stuff (I post at least) in any way that helps you! The content on this site uses the [CC 3.0 license](http://creativecommons.org/licenses/by-sa/3.0/) which means you can share/remix it as long as you attribute. – dirkgently May 30 '12 at 04:30