-
November 22nd, 2012, 06:12 AM
#1
Pointer to function type
Hi there, I'm fairly new to C++ and have begun working with pointers. I wish to create am array called sigmaf_point that reads data from a text file. I have managed to get that working, but when it comes to using this pointer I come across some problems. The array is created as such:
Code:
double sigma [5];
double *sigmaf_point = sigma;
void read(double *&sigmaf_point)
string s;
ifstream Dfile;
std::stringstream out;
out << 1;
s = out.str() + ".TXT";
Dfile.open (s.c_str());
if (Dfile.fail())
{
return;
}
for (int i=0; i<1; i++)
{
* * * * * * Dfile >> sigmaf_point[i];
}
}
I then create a coordinate system inside the main file, as the program I am writing is about modelling the movement of atoms, which requires you to know the coordinates:
Code:
int main();
double **coords_fluid = new double*[5000];
for (int i = 0; i < n_atoms_methane; i++)
{
coords_fluid[i] = new double[4];
}
Now, the problem arises when I want to calculate a new variable as so:
Code:
for (int i = 0; i <= n_atoms-1; i++)
{
sf1=sigmaf_point(coords_fluid[i][3]);
}
I get the error C2064: term does not evaluate to a function taking 1 arguments, and a red line under sigmaf_point that says it must be pointer to function type. I am a bit confused about this. Any help would be great.
-
November 22nd, 2012, 07:31 AM
#2
Re: Pointer to function type
You could make your work much easier it you got rid of using pointers and raw arrays and moved to std container classes (for example std::vector)
Victor Nijegorodov
-
November 22nd, 2012, 08:18 AM
#3
Re: Pointer to function type
 Originally Posted by lmd141
Now, the problem arises when I want to calculate a new variable as so:
Code:
for (int i = 0; i <= n_atoms-1; i++)
{
sf1=sigmaf_point(coords_fluid[i][3]);
}
I get the error C2064: term does not evaluate to a function taking 1 arguments, and a red line under sigmaf_point that says it must be pointer to function type. I am a bit confused about this. Any help would be great.
sigmaf_point is a pointer, not a function. If you look at the help for error C2064 that is exactly what is explained.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 22nd, 2012, 10:16 AM
#4
Re: Pointer to function type
 Originally Posted by lmd141
Hi there, I'm fairly new to C++ and have begun working with pointers. I wish to create am array called sigmaf_point that reads data from a text file. I have managed to get that working, but when it comes to using this pointer I come across some problems. The array is created as such:
Code:
double sigma [5];
double *sigmaf_point = sigma;
void read(double *&sigmaf_point)
string s;
ifstream Dfile;
std::stringstream out;
out << 1;
s = out.str() + ".TXT";
Dfile.open (s.c_str());
if (Dfile.fail())
{
return;
}
for (int i=0; i<1; i++)
{
* * * * * * Dfile >> sigmaf_point[i];
}
}
I then create a coordinate system inside the main file, as the program I am writing is about modelling the movement of atoms, which requires you to know the coordinates:
Code:
int main();
double **coords_fluid = new double*[5000];
for (int i = 0; i < n_atoms_methane; i++)
{
coords_fluid[i] = new double[4];
}
Now, the problem arises when I want to calculate a new variable as so:
Code:
for (int i = 0; i <= n_atoms-1; i++)
{
sf1=sigmaf_point(coords_fluid[i][3]);
}
I get the error C2064: term does not evaluate to a function taking 1 arguments, and a red line under sigmaf_point that says it must be pointer to function type. I am a bit confused about this. Any help would be great.
There is no need to make the code more complicated than it should be. The goal of C++ (according to the inventor, Stroustrup) is to make coding less conducive to bugs than the equivalent 'C' program. The introduction of pointers to do this work is overkill and not necessary at all.
As to Victor's suggestion, please take a look here:
Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
int main()
{
Double2D coords_fluid(5000, Double1D(4,0));
}
That one line of code does what your original main() program did. Instead of raw arrays, and new[] (which must be deleted at some point or else you get a memory leak), usage of the std::vector container class handles all of these details automatically.
Then this:
Code:
void read(Double1D& sigmaf_point)
{
if ( sigmaf_point.empty() )
return;
std::ifstream Dfile("1.txt");
if ( Dfile )
Dfile >> sigmaf_point[0];
}
This assumes that the proper headers are included and that the Double1D definition is visible. Note that there are no pointers here at all. You can then expand this code to make sure that the vector contains enough elements so that the read doesn't crash (which your original code cannot do since you're using pointers and not container classes such as vector, deque, or an MFC container class such as CArray or CList).
When you need to use pointers, you will know it since it will become obvious (for example, polymorphism, or you need to keep resources alive across function definitions or threads). However, to fake out a need for pointers is not good practice, unless you're purposefully making an academic exercise for yourself.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 22nd, 2012 at 10:47 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|