Jared Taylor
Jared Taylor

Reputation: 3

Header File Not Recognized

I've been working on a problem for class for over 6 hours, and have hit a confusing roadblock. Not sure what mistake I have made, and I've tried moving things, removing includes, adding includes, and even removing the following class.

The problem I've had so far is that the functions I made prototypes for in the header and then defined in the source file are not being recognized by the source.cpp in use.

This is all written in Visual Studio 2015 C++

File names are: monty.h and monty.cpp in order.

We are given two sample source.cpp files in order to test our work, however neither is giving me any results due to this error.

The main function provided states #include "monty.h" however the issue persists.

Header:

#ifndef MONTY_H
#define MONTY_H

class Monty{
private:
    int wins; // Number of Wins
    int ran; // Random Number Variable
    int val; // Value to determine win/loss via functions
    int x; // Determines number of times to repeat a function
    int count; // Counts number of times function repeated

    static int k(int); // 'Keep' choice function
    static int s(int); // 'Switch' choice function
    static int r(int); // 'Random' choice function

public:
    bool monty(char);
    int monty(char, int);
};
#endif

Source File:

#include "monty.h" // Including header file & other necessary libraries
#include <iostream>
#include <cstdlib>
using namespace std;

bool Monty::monty(char y){
    x = 1; // Setting count variable to 1 in order to prevent functions from repeating.
    if (y == 'k') // Calling functions depending on char input
        val = k(x);
    if (y == 's')
        val = s(x);
    if (y == 'r')
        val = r(x);

    if (val == 1) // Determining win/loss and returning value.
        return true;
    else
        return false;
}

int Monty::monty(char y, int z){
    x = z; // Initializing independent count variable
    wins = 0; // Initializing dependent wins variable
    count = 0;
    while (count < x) {
        if(monty(y))
            wins++;
    }
    return wins; // Returning number of wins
}

int Monty::k(int x) {
    int ran;
    ran = rand() % 3; // Obtaining a random number from 0 to 2
        if (ran == 1) {
            return 1;
        }
        else
            return 0;
}

int Monty::s(int x){
    int ran;
    ran = rand() % 3; // Obtaining a random number from 0 to 2
        if (ran == 1 || ran == 2) {
            return 1;
        }
        else
            return 0;
}

int Monty::r(int x) {
    int ran;
    ran = rand() % 2; // Obtaining random number from 0 to 1
        if (ran == 0)
            return k(x);
        else
            return s(x);
}

The two main functions we are given are written by the instructor, and as far as I can tell, are without error, as I seem to be the only student having this issue. It's likely I made a rookie mistake, but I doubt I'm the first.

Edits:

Exact Code for the error:

C3861 : 'monty': identifier not found - File: source.cpp - Line 18

Requested sample main file

// monty_test1.cpp - Test program #1 for Monty module

#include "monty.h"

#include <climits>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

static char get_strategy ();

int main () {

    // Initialize random number generator
    srand(time(0));

    cout << "Testing monty function..." << endl << endl;

    // Call monty function with user's choice of strategy, and report result
    if (monty(get_strategy()))
        cout << "Contestant won." << endl;
    else
        cout << "Contestant lost." << endl;

#ifdef _WIN32
    system("pause");
#endif

    return 0;
}

// Prompt for and input strategy, with validation
static char get_strategy () {

    // Get strategy, must be 's', 'k', or 'r'.
    char strategy;
    cout << "Enter strategy (s, k, r): ";
    cin >> strategy;

    // Validate input
    while (strategy != 's' && strategy != 'k' && strategy != 'r') {
        cout << "Stragegy is not valid. Try again." << endl;
        cin.ignore(INT_MAX, '\n');
        cin >> strategy;
    }

    // Discard any remaining input
    cin.ignore(INT_MAX, '\n');

    return strategy;
}

Solution found was removing the class referring to the monty functions as the main function could not be edited.

Requested Information about File Organization

Upvotes: 0

Views: 6281

Answers (1)

iksemyonov
iksemyonov

Reputation: 4196

A short answer, suggested by dxiv:

Provided that we can't change the last two files at all, we can declare a global function monty(char) and have it create a temporary object of class Monty, and then call its monty(char) function like this:

Add to monty.h:

bool monty(char c);

Add to monty.cpp:

bool monty(char c)
{
    return Monty.monty(c);
}

But that would anyway be a cludge, and not how classes are meant to be used.

A long answer:

// Call monty function with user's choice of strategy, and report result
if (monty(get_strategy())) { /*...*/ }

What exactly is monty? You obviously mean to call Monty::monty(char), but you forget to create an object of the class Monty first!

Should go like this:

int main () {
        // Initialize random number generator
    srand(time(0));    
    cout << "Testing monty function..." << endl << endl;

    Monty myMonty;
    ^^^^^^^^^^^^^^
    // Call monty function with user's choice of strategy, and report result
    if (myMonty.monty(get_strategy()))
        cout << "Contestant won." << endl;
    else
        cout << "Contestant lost." << endl;

#ifdef _WIN32
    system("pause");
#endif
        return 0;
}

To be more precise:

As noted above, you intend to call a non-static member function of the class Monty. Hence, you need to first create an object of such a class, and then call the function on that object. The compiler error you see occurs since you have not declared the monty variable anywhere before using it, hence cannot find identifier (identifier == the name of the variable in this context).

Edit per the OP's comment:

In C++, there really, really is no way to call a class' function, be it static or not, without mentioning the class' name or the class object's name. Thus, the expression monty(get_strategy()) implies that monty is either tha name of a global or local function, which is not what you've designed, obviously; or it is the name of an object with the overloaded operator(...), in which case we call it a functor object. Neither is the case with the code you've posted, so to make it work with the immutable professor's code, you want to redesign it a bit and make monty(char) a global function, instead of a public member function.

Upvotes: 1

Related Questions