Linker error (again) + constructors

w.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef W_H_
#define W_H_
#endif

class whateyva
{
    int neg;
    int pos;
    int tot;
    void bal(){ tot = neg + pos;}
    public:
    whateyva();
    whateyva(int x, int y);
    int addpos(int);
    int addneg(int);
    void stot();
};

^No errors here^
wdef.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "w.h"
using namespace std;

int whateyva::addpos(int x){
    pos += x;
    bal();
}

int whateyva::addneg(int x){
    x -= (x * 2);
    neg += x;
    bal();
}
whateyva::whateyva(){neg = 0; pos = 0; bal();}
whateyva::whateyva(int x, int y){pos = x; neg = y; bal();}
void whateyva::stot(){cout << tot;}

wuse.cpp
^Getting a linker error here, "undefined reference to WinMain@16"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include "w.h"
using namespace std;

int main()
{
    whateyva mood;
    do
    {
    int count = 0;
    cout << "Anything ";
    if(count > 0)
    cout << "else ";
    cout << "happen to you today? [y/n] > ";
    char occur;
    cin >> occur;
    occur = tolower(occur);
    if(occur == 'n')
    break;
    mood(0, 0);
    cout << "Good [g] or bad [b]? >";
    char ooo;
    cin >> ooo;
    if(ooo == 'g')
    mood.addpos(5);
    else
    mood.addneg(5);
}while(true);
    cout << stot() << " is your balance of moods today. ";
    cin.get();
    cin.get();
}

Saye stot() is undeclared, and
"no match for call to (whateyva) (int, int)"
I have absolutely no idea.
I've checked my sources and it appears my syntax is ok. Any ideas?


#include<iostream>
#include<cstdlib>

using std::cout;
using std::cin;

class whateyva
{
private:


int neg;
int pos;
int tot;
void bal()
{
tot = neg + pos;
}

public:

whateyva(int x=0, int y=0);
int addpos(int);
int addneg(int);
void stot();
};


whateyva::whateyva(int x,int y)
{
neg = y;
pos = x;
bal();
}


int whateyva::addpos(int x)
{
pos += x;
bal();
}

int whateyva::addneg(int x)
{
x -= (x * 2);
neg += x;
bal();
}


void whateyva::stot()
{
cout << tot;
}


int main()
{
whateyva mood;
do
{
int count = 0;
cout << "Anything ";
if(count > 0)
cout << "else ";
cout << "happen to you today? [y/n] > ";
char occur;
cin >> occur;
occur = tolower(occur);
if(occur == 'n')
break;

cout << "Good [g] or bad [b]? >";
char ooo;
cin >> ooo;
if(ooo == 'g')
mood.addpos(5);
else
mood.addneg(5);
}while(true);
mood.stot();
cout << " is your balance of moods today. ";
cin.get();
cin.get();
}
I noticed two problems:

wuse.cpp line 20: I don't think you can call a constructor that way; I believe you have to call it when creating the instance or not at all.

wuse.cpp line 29: stot() is not a global function, so you have to call it like so: mood.stot();
Putting it inside of a cout statement is also wrong since the function does the output itself and does not return a value.
^Getting a linker error here, "undefined reference to WinMain@16"


WinMain is the entry point to a windows GUI program. The code you have shown int main() is the entry point to a windows CONSOLE program.

I think you have got your windows types mixed up.
Fixed it! Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include "w.h"
using namespace std;

int main()
{
    whateyva mood(0, 0);
    do
    {
    int count = 0;
    cout << "Anything ";
    if(count > 0)
    cout << "else ";
    count++;
    cout << "happen to you today? [y/n] > ";
    char occur;
    cin >> occur;
    occur = tolower(occur);
    if(occur == 'n')
    break;
    
    cout << "Good [g] or bad [b]? >";
    char ooo;
    cin >> ooo;
    if(ooo == 'g')
    mood.addpos(5);
    else
    mood.addneg(5);
}while(true);
    mood.stot();
    cout << " is your balance of moods today. ";
    cin.get();
    cin.get();
}
Topic archived. No new replies allowed.