Zachary McCloud
Zachary McCloud

Reputation: 35

Main.cpp can't access variables and functions in header file and other .cpp file

main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include "cootie.h"

using namespace std;

int main()
{
    cout << "Time to create a Cootie!" << endl;
    cootie c;
    c.setName(name);
    cout << "Add body parts." << endl;
    cout << "1) Body" << endl << "2) Head" << endl << "3) Legs" << endl << "4) Wings" << endl << "5) Antennas" << endl << "6) Eyes" << endl;
    cout << "Input 7 to print.";
    while (roll != 7)
    {
        cin >> roll;
        if (roll == 1)
        {
            c.setBody(numBody);
        }
        if (roll == 2)
        {
            c.setHead(numHead);
        }
        if (roll == 3)
        {
            c.setLeg(numLeg);
        }
        if (roll == 4)
        {
            c.setWing(numWing);
        }
        if (roll == 5)
        {
            c.setAntenna(numAntenna);
        }
        if (roll == 6)
        {
            c.setEye(numEye);
        }
        if (roll == 7)
        {
            c.print();
        }
    }
}

cootie.h

#ifndef COOTIE_H
#define COOTIE_H
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class cootie
{
    public:
        cootie();
        int numLeg = 0, numHead = 0, numEye = 0, numWing = 0, numBody = 0, numAntenna = 0, roll = 0;
        string name = "Undefined";
        int setName(string& name);
        int setLeg(int& numLeg);
        int setHead(int& numHead);
        int setEye(int& numEye);
        int setWing(int& numWing);
        int setBody(int& numBody);
        int setAntenna(int& numAntenna);
        void print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna);
};

#endif // COOTIE_H

cootie.cpp

#include "cootie.h"


cootie::cootie()
{

}

int cootie::setName(string& name)
{
    cout << "Name your Cootie!" << endl;
    getline(cin, name);
}
int cootie::setBody(int& numBody)
{
    numBody++;
}
int cootie::setHead(int& numHead)
{
    numHead++;
}
int cootie::setWing(int& numWing)
{
    numWing++;
}
int cootie::setLeg(int& numLeg)
{
    numLeg++;
}
int cootie::setEye(int& numEye)
{
    numEye++;
}
int cootie::setAntenna(int& numAntenna)
{
    numAntenna++;
}
void cootie::print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna)
{
    cout << "Cootie called " << name << endl;
    cout << numLeg << " Leg(s)" << endl;
    cout << numHead << " Head(s)" << endl;
    cout << numEye << " Eye(s)" << endl;
    cout << numWing << " Wings(s)" << endl;
    cout << numBody << " Body(s)" << endl;
    cout << numAntenna << " Antenna(s)" << endl;
}

I keep running into an error that states that the variables and functions declared in cootie.h aren't able to be accessed in the main.cpp file. Basically it says that they aren't located in its scope. Any help would be great, i can't figure this out.

Edit: No longer getting the error of the functions and variables not being in the main.cpp scope, but now its saying that the "cootie c;" has an incomplete type.

Fixed it, I forgot to put in the arguments of my print function, everything works now, thanks for the help!

Upvotes: 0

Views: 3996

Answers (2)

Shoe
Shoe

Reputation: 76240

You have calls similar to this one:

setName(name);

of a non member function setName that is not defined. The function you probably meant to call is the member function of the class cootie. That should be called on an object of that class, like this:

cootie c;
c.setName(...);

Upvotes: 2

Nathan Wride
Nathan Wride

Reputation: 965

In your main.cpp try accessing them as:

cootie::<variable or function>

I also realized that those aren't static functions, you need to add a cootie object:

cootie myNewCootie;
myNewCootie.<functions and whatnot>

Upvotes: 0

Related Questions