Need help understanding struct, and a few errors w

2019-08-02 13:57发布

问题:

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

回答1:

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.


回答2:

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!