Error C2064 : term does not evaluate to a function using 1 arguments

A

Abhi

Hi all,

I am using .net for C++ and I would like to write some variable values
to some files.

I will be using that file in many member functions of the class. So I
declared the file variable names in class.

And I declared that to a file name in one member function.

Example:

class className
{
public:
void function();

private:
ofstream constRad;
}

void className::function()
{
constRad ("filename.txt");
}

and in other member functions I used this variable "constRad" to write
some variable values in the textfile.

But am getting an error as

"Error C2064 : term does not evaluate to a function using 1 arguments"

Could you please help me in this.

Thanks
Abbi.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

class className
{
public:
void function();

private:
ofstream constRad;
}

void className::function()
{
constRad ("filename.txt");
}

You wrote it as if you were calling a function named constRad. You want to
initialize it in the constructor:

// Note the name of the function; also, no return value
// (this is the constructor)

className::className()
:
constRad("filename.txt")
{}

Of course you need to declare the constructor in the class definition:

class className
{
/* ... */
className(); // constructor declaration
};

Ali
 
A

Abhi

Hi Ali,

Thanks for immediate reply.

I even tried that before, keeping it in the constructor.

But what actually I need is that, I would like to write the values to
file only for one of the two objects I have. So I kept the constructor
as shown below.


and only if bool is true,which I initialized for one object, I need to
write the values to the file.. so in the constructor, I wrote

constRad("filename.txt") in a "if" condition such as

class className
{
className(bool) //constructor
public:
....

private:
.....
}

int main()
{
className obj(true);
className obj2(false);
}


className::className(bool boolvar)
{
if(boolvar == true)
constRad("......txt");
}


and didnt write anything for else part...

and was still getting that error.. how to do it only for one object
(obj).. If I write that in just the constructor without any if
condition, then it will try to overwrite the samefile for different
objects.. though I dont try to write any variable values for the second
object(obj2), I dont know if that would be a problem.

Thanks,
Abbi.
 
A

Abhi

Hi Ali,

Small clarification,

In the above example code I wrote some things which are not similar to
what I did in my code, which you might think are the errors or which
may be the actual errors. that is in class declaration..Ofcourse those
should be there by default, but just wanted to let you know.

class className
{

public:
className(bool) //constructor
....
private:
.....
} ;

Also, I tried putting constRad(".....txt") outside the if condition, to
check if it doesnt give any errors. But still am getting the same
error..

Thanks,
Abbi.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

But what actually I need is that, I would like to write the values to
file only for one of the two objects I have. So I kept the constructor
as shown below.


and only if bool is true,which I initialized for one object, I need to
write the values to the file.. so in the constructor, I wrote

constRad("filename.txt") in a "if" condition such as

class className
{
className(bool) //constructor
public:
...

private:
....
}
[...]

className::className(bool boolvar)
{
if(boolvar == true)
constRad("......txt");
}

You are still using a syntax that looks like a function call, but you can do
it only in the constructor initialization list as in

className::className()
:
constRad("......txt")
{}

The problem with your constructor is that, once you are in the constructor
body, constRad is already default-constructed: it is a proper object that is
not associated with a file yet. You shouldn't use the initialization syntax
in the constructor body anymore.

You can open the file though:

className::className(bool boolvar)
{
if (boolvar) // <-- comparing with 'true' is not needed
{
constRad.open("......txt");
}
}

[...]

As an aside, it is better to introduce an enum to make the decision:

enum FileUseDecision { useFile, dontUseFile };

Once you have that, the code will be more clear:

className::className(FileUseDecision decision)
{
if (decision == useFile)
{
constRad.open("......txt");
}
}

Ali
 
A

Abhi

Hi Ali,

I am sending a small code which I wrote to just check this. And am
still getting the same error.

#include <iostream>
#include <fstream>

using namespace std;

class Abhi
{
public :
Abhi();
void filewrite();
private:
ofstream file;
};

Abhi::Abhi()
{
file("example.txt");
file<<"\n\n This is example\n\n";
}


int main()
{
Abhi a;
a.filewrite();
system("PAUSE");
return 0;
}


Could you please send me the correct syntax for this file so that I can
make use of that in my code accordingly as what you said in the
previous message.

Thanks,
Abbi.
 
A

Abhi

Okay, Problem solved !!!!

Thanks Ali and int2str for the help.. Now am able to do it without any
error...

Thanks once again..
Abbi..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top