Question:

Help me fix a simple(?) problem with C++ coding?

by  |  earlier

0 LIKES UnLike

I'm trying to learn C++ programing and can't for the life of me understand what the issue is with my current task (using Visual Studio 2008).

When I try to compile, I keep getting an error "C2228: left of '.printTime' must have class/struct/union"

Here's a snipit of the code:

//in main.cpp

int main()

{

baseTime time();

time.printTime();

}

//in baseTime.h

class baseTime

{

public:

baseTime();

void printTime() {

cout << ...

private:

int h;

int m;

}

To my knowledge, my code looks exactly the same as I've seen in book, in other forums, and in the VS help file, as well as exactly like programs I've used in the past the run perfectly well. What the heck is that error talking about and how do I fix it? Any suggestions?

 Tags:

   Report

3 ANSWERS


  1. what it doesnt like is this line:

    time.printTime();

    it doesnt know what &quot;time&quot; is

    try changing this line:

    baseTime time();

    to this

    baseTime time;

    when declaring the class as &quot;time&quot; you dont need the brackets.


  2. Aside from making an explicit constructor call on an object (the extra () that you don&#039;t need), you haven&#039;t defined the constructor for the class baseTime. You can probably get away with changing:

    baseTime();

    to

    baseTime(){};

    inside class baseTime if there&#039;s nothing you need to do in the constructor. Although it would be better form to define this in a separate baseTime.cpp file as:

    baseTime::baseTime() {}

  3. Sure about adding the Library ?

    Please check your Reference part.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.