I'm trying to learn about "user defined classes" and don't understand what they're for. Once I see a project compile it seems to click with me so I typed up the source file and matching "user defined class definition" that is in the user-defined classes chapter of my book. This file is a "counter class" of some sort. I put both the .cpp and .h files into one project and it didn't work. I was hoping to see what the advantage of a user-defined class was by seeing it work, but no luck. I tried another set that deals with "cube" I found on the net and it didn't work as well, so I must be missing something. As far as what I did to get these results: I added the header file by clicking on "add new item" under the tree. all that is there is a counter.h file and in the same project is a .cpp file which is included below. I have also included the info I found on the net; it is in the second half of this post.
Code:
#ifndef COUNTER_H
#define COUNTER_H
class counter
{
public:
counter();
counter(int);
void increment();
void decrement();
void setCount(int);
void setMaxValue(int);
int getCount() const;
int getMaxValue() const;
private:
int count;
int maxValue;
};
#endif // COUNTER_H
Code:
#include <iostream>
#include "header1.h"
using namespace std;
int main ()
{
counter c1;
counter c2(10);
c1.setCount(50);
c1.decrement();
c1.decrement();
c1.increment();
cout << "Final value of c1 is " << c1.getCount() << endl;
c2.increment();
c2.increment();
c2.decrement();
cout << "Final value of c2 is " << c2.getCount() << endl;
return 0;
}
Here are the errors that come up:
1>------ Build started: Project: classPractice, Configuration: Debug Win32 ------
1>Compiling...
1>classPractice.cpp
1>Compiling manifest to resources...
1>Linking...
1>classPractice.obj : error LNK2019: unresolved external symbol "public: int __thiscall counter::getCount(void)const " (?getCount@counter@@QBEHXZ) referenced in function _main
1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::increment(void)" (?increment@counter@@QAEXXZ) referenced in function _main
1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::decrement(void)" (?decrement@counter@@QAEXXZ) referenced in function _main
1>classPractice.obj : error LNK2019: unresolved external symbol "public: void __thiscall counter::setCount(int)" (?setCount@counter@@QAEXH@Z) referenced in function _main
1>classPractice.obj : error LNK2019: unresolved external symbol "public: __thiscall counter::counter(int)" (??0counter@@QAE@H@Z) referenced in function _main
1>classPractice.obj : error LNK2019: unresolved external symbol "public: __thiscall counter::counter(void)" (??0counter@@QAE@XZ) referenced in function _main
==================================================
here is the info for the cube project:
Code:
#ifndef CUBE_H#define CUBE_Hclass Cube{public: Cube(); ~Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();private: double Side;
};#endif
Code:
#include <iostream>
#include "cube.h"
Cube::Cube(){}Cube::~Cube(){}
void Cube::setSide(double s)
{
Side = s <= 0 ? 1 : s;
}
double Cube::getSide()
{
return Side;
}
double Cube::Area()
{
return 6 * Side * Side;
}double Cube::Volume()
{
return Side * Side * Side;
}void Cube::Properties()
{
cout << "Characteristics of this cube";
cout << "\nSide = " << getSide();
cout << "\nArea = " << Area();
cout << "\nVolume = " << Volume() << "\n\n";
}