C++ invalid use of nonstatic data member [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have looked around and have sen this question asked a fair bit but I the problems they are having seem different to mine.
I am only a beginner so I find it kind of hard to understand whats wrong with my program. Here is the code:
#include <string>
#include <iostream>
using namespace std;
class Character
{
int health;
string action;
public:
void setHealth(int hp) {health = hp;}
void setAction(string act) {action = act;}
int getHealth() {return health;}
string getAction() {return action;}
};
int main()
{
int difficulty;
Character player;
player.setHealth(15);
Character enemy;
cout << "What difficulty would you like to play? easy = 1, medium = 2, hard = 3 ";
cin >> difficulty;
switch (difficulty)
{
case 1 : enemy.setHealth(10); break;
case 2 : enemy.setHealth(15); break;
case 3 : enemy.setHealth(20); break;
}
cout << "\nEnemy health = " << enemy.getHealth << endl;
return 0;
}
And here is the error message I get: In function 'int main()':
36:39: error: invalid use of non-static member function
It appears the problem is at the cout at the bottom of the main function.
Please help!

getHealth() is a class method, not a member, so it should be:
cout << "\nEnemy health = " << enemy.getHealth() << endl;

Related

"Exception thrown: Write access violation in C++" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I get this error when I try to compile the following code: Exception thrown: write access violation.
this was 0xF81EFA0000.
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main() {
Demo obj[4];
int i;
for (i = 0; 1 < 4; i++)
{
obj[i].setX(i);
}
for (i = 0; i < 4; i++)
{
cout << "obj[" << i << "].getX(): " <<
obj[i].getX() << endl;
}
// Textual Data Types
// Boolean
return 0;
}
The error concerns line 10, where "x = i" is written. Any help would be good.
I haven't tried anything to fix it, other than writing brackets after "x", which obviously didn't work. I expect this program to print an array of objects when working properly.

Error in C++ code: In function 'int main(int, char**)': [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am a newbie and a student of Computer Sciences. I am doing my assignment Which is spell checker of a text file. I have done the code but I am getting the following errors. I am unable to resolve it. kindly help me guys. Thanks!
Here is my code:
#include<iostream>
#include<fstream>
using namespace std;
class spell_check
{
private:
int line_number=0;
string input="" ;
string dictionary="";
bool condition=false;
public:
void process(int x,char *y[]);
};
void spell_check::process(int x,char *y[])
{
ifstream input_file;
input_file.open(y[2]);
ofstream output_file;
output_file.open(y[4]);
while(!input_file.eof())
{
line_number++;
getline(input_file,input);
ifstream dictionary_file("dictionary.txt");
while(!dictionary_file.eof())
{
getline(dictionary_file,dictionary);
if( input.compare(dictionary) == 0 )
{
condition=true;
break;
}
}
if(condition==false)
{
output_file<<"**Spell mistake** "<< "( " << input << ")"<< "[" <<"at line no: " << line_number <<"]"<<endl;
}
dictionary_file.close();
condition=false;
}
cout<<"Successfully Write "<<endl;
input_file.close();
output_file.close();
}
int main(int argum,char *argu_array[])
{
spell_check SC;
SC.process(argum, *argu_array);
return 0;
}
there is an error here:
int main(int argum,char *argu_array[])
{
spell_check SC;
SC.process(argum, *argu_array);
return 0;
}
since the type of the second argument of the main function is char* []
and also the type of the argument of the process method is the same:
void spell_check::process(int x,char *y[])
you don't have to dereference it, try this:
SC.process(argum, argu_array);

'std::string' has no member named 'username' error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My code won't compile because of a 'std::string' has no member named 'username' error. I'm using code::blocks. I'm guessing the problem is because I'm trying to assign a string to a class, can someone help?
#include <iostream>
#include <string>
using namespace std;
class userx
{
public:
string username;
string password;
};
int main()
{
userx X, Y;
X.username = "X";
X.password = "1234";
Y.username = "Y";
Y.password = "5678";
string a;
string b;
cout << "What is your name?" << endl;
cin >> a;
if (a.username == a)
{
cout << "What is your password?" << endl;
cin >> b
if (a.password == b)
{
cout << "Password Accepted" << endl;
};
};
};
You wrote a.username when you probably meant X.username or Y.username.
if(a.username == a)
since a is string so it doesn't have username as its member that can be accessible using dot operator
Your code says
if(a.username == a){
cout<<"What is your password?"<<endl;
cin>>b
if(a.password = b){
cout<<"Password Accepted"<<endl;
};
when you declared string a; And indeed a std::string has no username.
Perhaps you meant to compare what was entered with X:
if(X.username == a){
cout<<"What is your password?"<<endl;
cin>>b
if(X.password = b){
cout<<"Password Accepted"<<endl;
};
Maybe you also mean to check against Y.
You might want to think about what happens if if the username is wrong. As it stands it will say password accepted even if the username is wrong.
Edit
In fact if(X.password = b) is an assignment not a comparison, so your user will probably get through regardless of username and password.
Edit again
It sounds like what you want is to use std::find on a container or to see if a std::map contains a given key using its find method. There are plenty of online clues

why am I getting random results when incrementing an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

Can't Print String Array Element [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".