New to OOP !!!


i got this in header file in x.h but how do you define that in the class .

I don't know How to do that !!

By the way I am using c++ !!!

 
   bool guess( string aNumberString ); 
What does your class look like? If you want to add bool guess(string); to a class, do it like this:
x.h
1
2
3
4
5
class X
{
public:
  bool guess( string aNumberString );
};


x.cpp
1
2
3
4
5
bool X::guess(string a NUmberString)
{
  // inplementation here
  return true;
}
Got the error says file is not recognized as an internal or external command c++ !!!
What does that mean !!!
Show us the code that is causing the error.
i dont know which Code that are causing it !!!
Your compiler will be telling you exactly which line of code is causing it.
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall BullsAndCows::BullsAndCows(void)" (??0BullsAndCows@@QAE@XZ) referenced in function _main
That's a link error. It's telling you that when you're linking your objects to create the executable, there's no definition for BullsAndCows::BullsAndCows(void).

Are you sure you've defined that constructor?
Thats my code for the Class

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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "logic.h"

#include <cstdlib>
#include <iostream>

void BullsAndCows :: start()
{
	int n = 9;
	while (n> 1)
	{
		int k = rand() % n;
		n--;
		swap(fSecretNumbers[n], fSecretNumbers[k]);
	}
	fSecretNumberIndex = 0;
}

bool BullsAndCows ::guess (string aNumberString)
{
int lBulls = 0;
int lCows = 0;
for (int i=0;i<4;i++)
	{
		int lTest = int(aNumberString[i]) - '0';
		if (fSecretNumbers[i] == lTest) {
			lBulls = lBulls + 1;
		}
		else
			for (int j=0;j<4;j++)
			{
				if (fSecretNumbers[j] == lTest){
					lCows = lCows + 1;
					break;
				// terminate for loop
				}
			}
	}
cout <<  "Number of bulls: " << lBulls;
cout << ", number of cows: " << lCows;
cout <<endl;

if (lBulls == 4){
	return true;
}
else
	return false;
}
It would help if we could see the class definition too.
ok
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include <String>
using namespace std;
class BullsAndCows
{
private:
	int fSecretNumbers[9];
	int fSecretNumberIndex;
public:
	BullsAndCows();
	void start ();
	bool guess( string aNumberString );
};

Well, there you go. You've declared a default constructor, but not defined one. The linker can't find it because it's not there.
I set No to linking !!!
Hi there,

Just as a kindly remark: Please don't use exclamation marks (!) after every sentence. In many cultures this is perceived as shouting and thus rude. I'm not saying you are intending to be rude, I'm just saying it's something you should probably be aware of.

As to your issue - what do you mean by:

I set No to linking !!!



The problem can be easily fixed as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once

#include <String>
using namespace std;
class BullsAndCows
{
private:
	int fSecretNumbers[9];
	int fSecretNumberIndex;
public:
	BullsAndCows() =default;  //we accept the default constructor implementation of the compiler
	void start ();
	bool guess( string aNumberString );
};


As a small remark: note that #pragma once is not supported by all pre-processors. Thecross-platform way of doing this is to use include guards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BULLS_AND_COWSH //if this was not defined already
#define BULLS_AND_COWSH //define it, so the check above will fail next time

#include <String>
using namespace std;
class BullsAndCows
{
private:
	int fSecretNumbers[9];
	int fSecretNumberIndex;
public:
	BullsAndCows() =default;
	void start ();
	bool guess( string aNumberString );
};

#endif //close ifndef 


All the best,
NwN
Last edited on
Topic archived. No new replies allowed.