c++ when using functions function call missing argument [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 8 years ago.
Improve this question
void Circle::getXYr() {
cout << "(x,y,R) = (" << x << "," << y << "," << r << ")" << endl;
}
double Circle::circArea() {
return (r * r * PI);
}
double Circle::circPeri() {
return (2 * r * PI);
}
void Circle::printCircle() {
cout << "Printing circle " << getXYr << endl;
cout << "Circle Area: " << circArea << endl;
cout << "Circle Perimeter: " << circPeri << endl;
}
I cant use the printCircle function: ERROR Error 1 error C3867: 'Circle::getXYr': function call missing argument list; use '&Circle::getXYr' to create a pointer to member
also the same for the next 2 functions.

It are functions, you have to call them as such. Note the brackets ()
cout << "Printing circle " << this->getXYr() << endl;
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;
this-> is not explicitly needed.
Your second error is due this->getXYr() does not return a value. It returns void. There is no basic_ostream overload for it.
You either should put the logic of the getXYr() into the printCircle() function. Or call that function without calling cout on it:
cout << "Printing circle ";
this->getXYr();
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;

Related

error: expected ‘;’ before ‘generationString’ [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 4 years ago.
Improve this question
I'm currently trying to work out some issues I am experiencing with this code, can't really figure out why I am getting these 2 errors.
I tried to see if something was not closed, but this not does seem to be the case, can be be cause of the distance between the ": "?
I'm just grasping for straws by now..
main.cpp:30:38: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x << ": " generationString << endl;
main.cpp:54:40: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x++ << ": " generationString << endl;
When trying to compile this code:
#include <iostream>
using namespace std;
string
initString ()
{
}
int
calculateScore (string guess, string target)
{
}
string
mutate (string mutationString)
{
}
int
main ()
{
string targetString = "METHINKS IT IS LIKE A WEASEL";
string generationString = initString ();
string currentString = generationString;
int score = calculateScore (currentString, targetString);
int x = 0;
cout << "Generation " << x << ": " generationString << endl;
do
{
for (int i = 0; i < 100; i++)
{
string newCopy = generationString;
newCopy = mutate (newCopy);
int copyScore = calculateScore (newCopy, targetString);
if (copyScore > score)
{
currentString = newCopy;
score = copyScore;
if (copyScore == targetString.length ())
{
break;
}
}
}
generationString = currentString;
}
while (score < targetString.length ());
cout << "Generation " << x++ << ": " generationString << endl;
return 0;
}
You're missing a <<.
cout << "Generation " << x << ": " generationString << endl;
Should be
cout << "Generation " << x << ": " << generationString << endl;
Likewise for
cout << "Generation " << x++ << ": " generationString << endl;
That should be
cout << "Generation " << x++ << ": " << generationString << endl;
You are probably missing a << in the line
cout << "Generation " << x << ": " generationString << endl;
Here you have
": " generationString
which should be
": " << generationString
C++ can concatenate literal strings, but it cannot concatenate literal string with anything else (like std::strings). So for instance this would work
cout << "Generation " << x << ": " "METHINKS IT IS LIKE A WEASEL" << endl;

Why does spi2 point to a different address as spi when using copy constructor [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
i need to get a byte-representation of an integer, so im trying to create a char* from a standard int. Why does spi2 point to a different address as spi when using copy constructor.
int main(int argc, char* argv) {
std::cout << "[dbg0]" << std::endl;
int foo = 2100000000;
std::cout << "&foo: " << &foo << " - val: " << foo << " - sizeof: " << sizeof(foo) << std::endl;
std::shared_ptr<int> spi = std::make_shared<int>(foo);
std::cout << "&spi: " << &spi << " - val: " << *spi << std::endl;
std::shared_ptr<int> spi2(spi);
std::cout << "&spi2: " << &spi2 << " - val: " << *spi2 << std::endl;
int* bar = &foo;
std::cout << "&bar: " << &bar << " - val: " <<*bar << std::endl;
bar -= 8;
std::cout << "&bar-8: " << &bar << " - val: " <<*bar << std::endl;
char* bytewiseint = (char*)&bar;
std::cout << "&bytewiseint: " << &bytewiseint << " - val: " <<*bytewiseint << std::endl;
char* bytewiseint2 = reinterpret_cast<char*>(&foo);
std::cout << "&bytewiseint2: " << &bytewiseint2 << " - val: " <<*bytewiseint2 << std::endl;
std::cout << "[dbg1]" << std::endl;
for(unsigned int c = 0; *(bytewiseint+c) != '\0'; c++) {
std::cout << "c=" << c << ". " << *(bytewiseint+c) << std::endl;
}
std::cout << "[dbg2]" << std::endl;
std::getchar();
return 0;
First you have:
int foo = etc...;
The address of this int is &foo.
When you use make_shared using foo, a new foo is created by make_shared and you get a shared_ptr object returned. This object contains the address of the foo created by make_shared and not the foo you created.
I see where you:
bar -= 8;
and then you:
cout << &bar ...
This is the address of bar and not the address inside bar. The address of bar does not change when you change the contents of bar. The contents of bar is *bar. If you cout that you will see the address contained in bar.
This is why &sp1 and &sp2 are different. They are different objects. You need to look inside these object to see the pointer they own.

C++ outputting weird [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 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
cout << "*****************************************" <<
endl <<
cout << "Hello All!" <<
endl <<
cout << "Welcome to CSCI-111!!!!!" <<
endl <<
cout << "It is great to see you!" <<
endl <<
cout << "*****************************************" ;
return 0;
}
The first cout is fine and outputs correctly, but every cout after that outputs a strange string of numbers before the words in quotation marks (0x600e88) and my output ends up looking like this
*****************************************
0x600e88Hello All!
0x600e88Welcome to CSCI-111!!!!!
0x600e88It is great to see you!
0x600e88*****************************************
What you currently have:
cout << "blah" << endl << cout << "blah" << endl << cout << ... ;
// ^~~~ ^~~~
You're printing the cout itself, this is what gives you weird numbers.
What you should have:
cout << "blah" << endl;
cout << "blah" << endl;
Or:
cout << "blah" << endl
<< "blah" << endl;
Terminate endls:
cout << "*****************************************" << endl;
cout << "Hello All!" << endl;
cout << "Welcome to CSCI-111!!!!!" << endl;
cout << "It is great to see you!" << endl;
cout << "*****************************************" ;
Or delete the redundant couts:
cout << "*****************************************" << endl <<
"Hello All!" << endl <<
"Welcome to CSCI-111!!!!!" << endl <<
"It is great to see you!" << endl <<
"*****************************************" ;
Otherwise, the expression continues, and you print cout itself, and since it's a function pointer, you print it's address (0x600e88).
In the sequence
cout << "Something" << endl << cout;
the first cout indicates the start of ostream (a stream printing out to the console), while the second one is the part of the stream you want to output, and is treated as a pointer, who outputs what he contains - a numeric address of the call to cout.

cout.setf(ios::fixed) and cout << fixed do not seem to get the same result? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
this is my sample main :
double hours = 35.45;
double rate = 15.00;
double tolerance = 0.01000;
cout.setf(ios::scientific);
cout << "Scientific notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
<< hours * rate << ", tolerance = " << tolerance << endl << endl;
cout.setf( ios::fixed ); // if i replace but cout << fixed, it works
cout << setprecision( 3 );
cout << "Fixed decimal notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
<< hours * rate << ", tolerance = " << tolerance << endl << endl;
system( "pause" );
Why are they not equal? if they are equal then what is wrong here?
The flag std::ios_base::fixed is just one of two flags in std::ios_base::floatfield. The function std::ios_base::setf() is oblivious of the relationship between the different flags and merely sets a bit pattern. If you want to set std::ios_base::fixed and clear other fields from the subgroup, you'd use
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
When you use the manipulator std::fixed it is equivalent to this call.

Can't use void() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i'm having an issue with my C++ code:
#include <iostream>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
void displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints) // Call this function to display
{ // ennemy related informations
cout << endl << "Ennemy health points: " << ennemyHealthPoints;
cout << endl << "Ennemy attack points: " << ennemyAttackPoints << endl;
}
int main()
{
//Player related variables
int healthPointsMax(100);
int actionPointsMax(100);
int recoveryPoints(100);
int healthPoints(100);
int actionPoints(100);
int attackPoints(100)
//Player related variables
//Ennemy related variables
int ennemyHealthPoints(230);
int ennemyAttackPoints(10);
//Ennemy related variables
//Main variables
string stringInput;
//Main variables
//TEXT
cout << "HP: " << healthPoints << endl;
cout << "AP: " << actionPoints << endl;
cout << "RP: " << recoveryPoints << endl;
cout << endl;
cout << "HP = Health Points, AP = Action Points, RP = Recovery Points" << endl;
cout << endl;
cout << "CONTROLS:" << endl;
cout << "attack [ennemy name] //attacks the ennemy" << endl;
cout << "heal [playername] // heals the selected player" << endl;
cout << endl;
cout << "A wild nugget appears!" << endl;
cout << endl;
cout << "What do you want to do?" << endl;
// TEXT
getline(cin, stringInput);
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);
}
return 0;
}
The debugger says that there is a problem with the fonction "void displayEnnemyStatus"
I checked every variable, there is no problem with that.
Am I doing something wrong?
Try :-
if (stringInput == "attack ennemy")
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
**displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints)**;
}
Instead of :-
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);