Why is my function "missing argument lists"? - c++

I'm not sure what I've done wrong in my project. I have two files, an airports.h header file and a main source file. In the main source file I have
string code,name;
int deptax, conntime;
cin >> code >> name >> deptax >> conntime;
Airport myAirport(code,name,deptax,conntime);
cout << myAirport.getCode << endl;
and in the airports.h header file I have
class Airport{
public:
Airport(string code, string name, int departureTax, int connectionTime)
:code(code),
name(name),
departureTax(departureTax),
connectionTime(connectionTime)
{...}
string getCode(){
return code;
}//then getName, getDepTax, getConnTime...
}
When I run the main source file, I get the error "error C3867: 'Airport::getCode': function call missing argument list; use '&Airport::getCode' to create a pointer to member" which is in line 5 up there.
I'm a beginner so I'm not sure why it's telling me to do this. Shouldn't .getCode() work how it's written? When I looked for previous solutions to this online, the solution was always something that was unrelated to the "pointer to member" error, so I think I may simply be using c++ in a way that it's not meant to be used.

cout << myAirport.getCode() << endl;
Note the parenthesis needed to call the function. The reason for requiring the () to be there is that a function named without them actually has a valid meaning and it is quite different to calling the function.

You need to call your function
myAirport.getCode()
instead of
myAirport.getCode

You're missing the () in the
cout << myAirport.getCode << endl;
after getCode ,should be getCode()

Related

Saving a struct array to an external file in c++

I have an assignment where I need to:
save the list that the user inputs to an external file.
load the info from the file previously saved.
I managed to write in the code for the 1st task, but since I have errors, I couldn't continue to the 2nd task. Please take a look and let me know what your thoughts are.
First Error:
When you create an array, the name of the array is a pointer to the beginning of where the array is in memory. In line 42, you cannot compare an int with a pointer like that. Instead, I assume you want to do this:
for (int i = 0; i < size; ++i) {
Second Error:
In line 43, you are trying to input an std::ofstream object into a function. In order to do this, std::ofstream must be copy-able. ofstream has a deleted copy constructor, meaning that it cannot be copied and thus cannot be passed as an input to a function. Instead, you could simply create the ofstream object and open the file within your pet::save function. Also, make sure you close the ofstream. As an example:
void pet::save()
{
ofstream file;
out.open("animal.txt");
if (!out.is_open())
cout << "Unable of open file." << endl;
file << pet_info << endl;
file.close();
}
You could also use a pointer to the ofstream as an input to your save function, since pointers can be copied (then use out->operator<<(pet_info) to input to the file). This would make it run faster, but this situation does not seem to prompt such optimization. The function prototype would look like
void pet::save(ofstream* file);
and you would pass &out as the input to the function.
Third Error:
You are trying to use the array called animal within your pet class. Since animal is an array of pets that is created outside of your pet class, the pet class does not have access to it (so animal was not declared in the scope of pet). I am guessing your pet class stores a string which contains a pet's information (which I call pet_info). Given that is true, you can call the above save function that I wrote for all of your pets in the animal array to save them to a file.
Fourth Error:
On line 109 of pet.cpp, it appears you are missing a semicolon. That could be why the bracket error is there, or you are just missing a bracket.

error: statement cannot resolve address of overloaded function C++

I am studying C++ by book and was trying to test simple coding for a looped game combat. A switch inside a while loop.
damage = greatsword[0] + player[1] - enemy[2]; endl;
error: statement cannot resolve address of overloaded function|
I have this error in 4 different code lines and in each one it has 'damage' so I assume its some problem with that. I have damage declared as an int and set to 0 before trying to change it to the attack value. I can provide more of the code if needed. I also tried changing the name from x to dmg to see if that was the problem
You have a trailing endl; which you probably did not mean to include. std::endl is a function which is used when printing to an output stream; usually you'll see cout << ... << endl;, but otherwise it should not be used.
std::endl, as used as a stream manipulator, is actually a function.
You probably achieve a similar or related error message by doing this
void f(); // don't need to define f()
int main()
{
f; // should be f() to call the function
}
So remove the statement endl;

Making a variable that has code inside of it?

Is there a way I can make a variable in C++ that has code inside of it that I can run later?
For example:
string code = "cout << "Hello World" << endl;"?
Or make a file or Notepad file that can be run later? Like if someone adds an employee to a program, it'll make a file and add all of the employee's information. Then later on when the program user says "I want to see that employee" by either writing their name or a unique ID, it shows all of the employee's information.
Thank you.
The solution is to use #define preprocessor directive to create a macro:-
#define code cout << "Hello World" << endl;
Now whereever you use the word 'code', it will be replaced by cout << "Hello World" << endl;
eg:
int main()
{
code
return 0;
}
Yes, it's relatively new syntax.
std::string msg = "Hello world";
auto code = [&std::cout, =msg]() {std::cout << msg;};
// Now we can pass code about as a variable.
code(); // runs it, even if std::cout and msg has gone out of scope
The advantage is that a comparison function, or a key access function, or similar can be attached to a bigger structure. You can also build entire programs out of lambdas, but that's getting rather theoretical.
However you must specify the C++ code at compile time. You can't compile C++ on the fly (yet).

Classes reading one word from file C++

This is my program and it supposed to get text word by word from file Team.txt but it says it can't open the file. I tried showing directory directly to the file still the same answers. I think I got something here:
class Team
{
public:
string name;
string dificulty;
string section;
};
void GetTeamInfo(class Team);
int main()
{
Team ko;
GetTeamInfo(ko);
cout << ko.name;
cout << ko.dificulty;
cout << ko.section;
system("PAUSE");
}
void GetTeamInfo(Team)
{
std::ifstream fd;
fd.open("Team.txt");
Team ko;
if (fd.is_open())
{
fd >> ko.name;
fd >> ko.dificulty;
fd >> ko.section;
}
else
{
std::cout << "Mistake can't open file 'Team.txt'\n";
}
}
It doesn't work because your argument handling is all wrong.
The declaration
void GetTeamInfo(Team)
tells the compiler that GetTeamInfo is a function which takes an argument of type Team, but you don't give it a name so you can't use the argument anywhere inside the function.
If you want to use the passed argument you should give it a name:
void GetTeamInfo(Team ko)
You then don't have to declare the variable ko inside the function.
However, this is not going to work anyway, because arguments are by default passed by value, that means arguments are copies of the values from the caller. And changing a copy will of course not change the original. So what you should do it pass the argument by reference:
void GetTeamInfo(Team& ko)
All of this is very basic C++ knowledge, and any good book or tutorial should have learned you this very early.
As for the problem of the program not being able to open your file, there are multiple possible causes for that problem:
Your IDE (Integrated Development Environment) is having one current directory for your program, and the file is not in that directory. This can be changed with project settings.
The file is actually not where you think it is.
The file simply doesn't exist.
On systems such as Linux and Mac OSX filenames are case sensitive, and the actual file doesn't have a capital 'T' in its name.

C++ create ifstream/ofstream in a class, associated with pre-made text file

I am having a problem associating an ifstream read and ofstream print to a pre-made text file called finances.txt. This is within a class called Data. So far, this is what I've tried:
I declared ifstream read and ofstream print in the class header file. Then, in the cpp file:
Data::Data(string n, string d)
:name(n),
date(d)
read(name)
print(name)
{
cout << "name = " << name << endl;
read.open(name);
print.open(name);
//...
}
I also tried this, without declaring anything in the header:
Data::Data(string n, string d)
:name(n),
date(d)
{
ifstream read(name);
ofstream print(name);
//...
And just different variations of this kind of thing. The syntax is always correct in the sense that I don't get any errors, but whenever it runs, it acts like the file doesn't exist and creates a new one named finances.txt, which in turn erases all of the text that was in the original. I have done this correctly before and just can't remember what I did and what I am doing incorrectly here.
I am a little confused as to what exactly you are trying to do?
Are you trying to append to the file? Because when you call
ofstream print(name) you are writing over the file that you are reading in.
So if you want to append to that same file you have to add.
fstream::app in the declaration of the ofstream