C++ Creating & Using Class

Go To StackoverFlow.com

0

I have to create a class for an assignment, I've done all that I can & I've done the research and I have read my textbook. What else do I need to do to my classes to make what is in my Main run? Everything you need to know is in the code description.

/*  LAB07.cpp
    ALEXANDER YHAP
    04/2012

    In this lab you will create a new class called LabMetaData. Objects of this
    class could be used in future lab assignments to store information about 
    the lab itself. 

    An object of class LabMetaData has the following attributes:
    .   Lab Number - A whole, positive number. Zero is valid.
    .   Lab Title - A title for the Lab Assignment
    .   Lab Author - The name of the programmer that wrote the lab.
    .   Lab Data - The date the lab was written, stored as three integer 
        numbers. The Day must be between 1 and 31. The month must be between 1 
        and 12. The year must be 4 digits and in the 21st Century (between 2000 
        and 2099).
    .   Lab Description - A description of the Lab Assignment.

    An object of class LabMetaData has the following methods:
    .   Constructor - set the Lab Number to zero, the Lab date to 1/1/2010, 
        and all other attributes to empty strings. (Hint: call the SetData() 
        from the constructor function to avoid duplicating your code)
    .   SetData() - sets the attributes of the object to the parameters as long 
        as the parameters are valid. Rules: 
        o   ALL of the parameters must be valid in order for ANY of the 
            attributes to change. 
        o   Validation rules are explained above for Lab Number and Lab Date. 
            Title, Author, and Description have no validation.
        o   If no problems are detected, return TRUE. Otherwise return FALSE.
    .   ShowData() - displays all the object's attributes on the console. 

    The main() function and a sample executable is provided. 
*/

#include <iostream>
using namespace std;
//Class Declaration Section
class LabMetaData 
{
    private:
    int labNum;
    string labTitle;
    string labAuthor;
    int Month;
    int Day;
    int Year;
    string labDesc;    

    public:    
//    LabMetaData(int labNum, string labTitle, string labAuthor,int Month, int Day, int Year, string labDesc); //constructor
    LabMetaData(int = 0, string = "Empty Title", string = "Empty Author",int = 01, int = 01, int = 2012, string = "Empty Description");
    void LabMetaData::SetData(int, string, string, int, int, int, string);
    void LabMetaData::ShowData();
};
//Class Implementation Section
LabMetaData::LabMetaData(int Num, string Title, string Author, int MM, int DD, int YYYY, string Desc)
{
    labNum = Num;
    labTitle = Title;
    labAuthor = Author;
    Month = MM;
    Day = DD;
    Year = YYYY;
    labDesc = Desc;
}

void LabMetaData::SetData(int Num, string Title, string Author, int MM, int DD, int YYYY, string Desc)
{
//    labNum = 7;
//    labTitle = "N/A";
//    labAuthor = "Unknown";
//    Month = 01;
//    Day = 01;
//    Year = 2012;
//    labDesc = "N/A";
//    return;
    labNum = Num;
    labTitle = Title;
    labAuthor = Author;
    Month = MM;
    Day = DD;
    Year = YYYY;
    labDesc = Desc;
    return;
}

void LabMetaData::ShowData()
{
     cout << "Lab " << labNum << ": " << labTitle << endl;
     cout << "Created by: " << labAuthor << endl;
     cout << "Date: " << Month << "/" << Day << "/" << Year << endl;
     cout << "Description: " << labDesc << endl;
     cout << endl;

     return;
}

int main()
{

    LabMetaData Lab7; 

    cout << endl << "Uninitialized: " << endl;
    Lab7.ShowData();

    Lab7.SetData(7,
    "Introduction to Classes",
    "Alexander Yhap",
    10, 3, 2010,
    "In this lab you will create a new class called LabMetaData. Objects of this class could be used in future lab assignments to store information about the lab itself.");

    cout << endl << "Intialized: " << endl;
    Lab7.ShowData();

    if(!Lab7.SetData(-1, "Test", "Test", 13, 32, 11, "Causing Errors"))
        cout << "\nErrors!" << endl;

    cout << endl << "After Invalid Modification Attempt: " << endl;
    Lab7.ShowData();

    cout << endl << endl;
    system("pause");
    return 0;
}

The error messages are:

prog.cpp:32:27: error: no 'void LabMetaData::SetData()' member function declared in class 'LabMetaData'
prog.cpp:44:28: error: no 'void LabMetaData::ShowData()' member function declared in class 'LabMetaData'
prog.cpp: In function 'int main()':
prog.cpp:58:17: error: no matching function for call to 'LabMetaData::LabMetaData()'
prog.cpp:21:1: note: candidates are: LabMetaData::LabMetaData(int, std::string, std::string, int, int, int, std::string)
prog.cpp:5:1: note:                 LabMetaData::LabMetaData(const LabMetaData&)
prog.cpp:61:10: error: 'class LabMetaData' has no member named 'ShowData'
prog.cpp:63:10: error: 'class LabMetaData' has no member named 'SetData'
prog.cpp:66:10: error: 'class LabMetaData' has no member named 'ShowData'
prog.cpp:68:9: error: 'Lab4' was not declared in this scope
prog.cpp:72:10: error: 'class LabMetaData' has no member named 'ShowData'
2012-04-04 17:29
by user1087935
Where are you stuck? Does the code compile? If not, what are the compiler errors - André Caron 2012-04-04 17:31
You should tell us what the problem is, not the other way around. What is the full compiler error message? If you're using Visual Studio, that's in the "Output" window, not the "Error" window - Mooing Duck 2012-04-04 17:31
I ran your code through gcc-4.5.1 and added those error messages to your question for you - Mooing Duck 2012-04-04 17:32
@user1087935: Additional information goes in the question, not in the comments. Comments can be and are deleted without warning - Mooing Duck 2012-04-04 17:34
I updated the code...it is all working so far except for the if. It gives a could not covert to bool error. how can i fix this problem without changing the code in the int Main - user1087935 2012-04-04 20:11


1

You need to add the method declarations inside the class definition. There is a case mismatch:

class LabMetaData 
{
//....
    void setData(int, string, string, int, int, int, string); // should be SetData
    void showData(); // should be ShowData
};

You're also missing a default constructor for the class:

class LabMetaData 
{
//....
   LabMetaData(); // <-- default constructor
};

therefore you can't do:

LabMetaData Lab7; 

since this attempts to call the missing default constructor. Either define one or pass parameters to the constructor.

2012-04-04 17:34
by Luchian Grigore
I see them there, the OP just used a lowercase letter to begin the name and an uppercase letter when calling it in main - Ed S. 2012-04-04 17:35
@EdS. yup, he's also missing a default constructor - Luchian Grigore 2012-04-04 17:37
okay, i think my classs is good to go now - user1087935 2012-04-04 17:44
my teacher gave us that sample code & i guess he expects us to use it. i just dont understand it - user1087935 2012-04-04 17:45


1

(1) The first error message: error: no 'void LabMetaData::SetData()' member function declared in class 'LabMetaData' means you should look really close at your function setData and make sure they match:

void              setData(int, string, string, int, int, int, string);
                  |       |
void LabMetaData::SetData()

I note here, you got the capitalization wrong, and in the definition, did not have parameters. These must match. Your showData function is also has the capitalization wrong.

(2) The error message: error: no matching function for call to 'LabMetaData::LabMetaData()' means your class did not receive an automatic default constructor, (since you gave it a constructor that requires parameters), and so it doesn't know how to create one properly at the line:

LabMetaData Lab7; 

So you'll either have to construct this with parameters, or provide a default constructor:

void LabMetaData::LabMetaData() {
    //stuff
}

All the rest of your errors are due to these two issues.

(3) Responding to your comment, you get the error about Lab4 because the of the line:

if(!Lab4.SetData(-1, "Test", "Test", 13, 32, 11, "Causing Errors"))

But you never created an object called Lab4. Did you mean to call a function on the object Lab7? Also, you said that the SetData function shouldn't return anything. I don't see how it could fail without throwing an exception anyway, so I don't think you need that if statement at all.

2012-04-04 17:37
by Mooing Duck
Okay, so I got my class working now. it's just the issue of declaring my class and calling it Lab - user1087935 2012-04-04 17:49
getting whats in the int Main to wor - user1087935 2012-04-04 17:49
@user1087935: I edited the question to address that - Mooing Duck 2012-04-04 17:52
yes it is supposed to be lab7 instead of lab4 thank you. but i still need to inculde that if statment. the error im getting is could not convert to bool...so how do i fix this. do i have to create a variable in the class and gives a true or flase value? or what do i do? help - user1087935 2012-04-04 20:15
btw i update the working code so far. only thing that isnt working now is that if statement - user1087935 2012-04-04 20:15
@user1087935: you wrote that SetData should not return anything. If you want it to return an error condition of some sort, you'll have to alter the function to actually return something. Though I don't see how anything there could fail, except for std::bad_alloc. As such, you don't really need the if statement - Mooing Duck 2012-04-04 20:20
all the Code in the int Main was given to me by my teacher. we are supposed to make the class work with the code provided - user1087935 2012-04-04 20:48
@user1087935: The first half of that comments says "If you want it to return an error condition of some sort, you'll have to alter the function to actually return something." Probably a bool in this case - Mooing Duck 2012-04-04 21:10


0

C++ is case sensitive. setData is not the same as SetData. You need to call it as you defined it.

2012-04-04 17:34
by Ed S.
thanks! I always have this problem I have to pay more attention to simple errors like this in my code - user1087935 2012-04-04 20:20
Ads