Quick Answer Please. Friend function. Remove one ERROR.

Pages: 12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
using namespace std;

#define LEN 100
////////////////////////////////////////////////////////////////////////////////////////////////////
class employee{
private:
	char name[LEN];
	unsigned long number;
public:
	friend istream& operator >> (istream& s, employee& e);
	friend ostream& operator << (ostream& s, employee& e);
};
//--------------------------------------------------------------------------------------------------
istream& operator >> (istream& s, employee& e){
	cout << "Name: ";	s.getline(e.name,LEN,'\n');

	return s;
}


ERROR:
--------------------Configuration: hgggh - Win32 Debug--------------------
Compiling...
c.cpp
D:\VC 6.0\hgggh\c.cpp(17) : error C2248: 'name' : cannot access private member declared in class 'employee'
D:\VC 6.0\hgggh\c.cpp(9) : see declaration of 'name'
Error executing cl.exe.

c.obj - 1 error(s), 0 warning(s)

The compiler says it all. You can't refer to e.name outside of your class because name is private. You will need an accesser function.
I've made >> as friend function in the class at line 12.
Last edited on
Friend functions have access to the objects passed as arguments.
That's nice; however, the problem isn't with the friend function, the problem is the call to getline():

1
2
3

s.getline( e.name, LEN, '\n' );
How to solve this issue?
When I do s >> e.name; Error remains. So I don't think that problem is here.
I already told you: Write an accesser function.
No I don't want to have an accessor function. What will be the benefit of using a friend function then?
OK, make it a friend accessor function.
What's "friend accessor function"?
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
/*    Add this as a public function to your class:    */

const char* GetName() const
    {
    return Name;

    }

/*    Call it like this:    */

cout << "Name: " << s.getline( e.GetName(), LEN, '\n' );
No use of FRIEND FUNCTION THEN.
Your code is valid and should be compiled. I advice to substitute this old C++ compiler for a new one.

Last edited on
That's what I was thinking as well. I love you vlad. I was almost gone mad.
At least you can test the code on-line at www.ideone.com
Can you give me link to download a good one, or the one which you use? Please...
You can download MS VC++ 2012 Express Edition from the Microsoft site. I have no the exact link now but hope somebody will point it.
hi
i need to load a ply file in opengl and i find this code:
/* Demonstrates how to load PLY files
* Needs some refactoring.
*
* Model needs to be triangulated
* Use blender
*
* Just the class for loading PLY files.
*
*/

#include <windows.h>
#include <stdio.h>
#include <string.h>
//#include <GL/gl.h>
#include <GL/glut.h>
#include <cmath>
#include <string>


class Model_PLY
{
public:
int Model_PLY::Load(char *filename);
void Model_PLY::Draw();
float* Model_PLY::calculateNormal( float *coord1, float *coord2, float *coord3 );
Model_PLY();

float* Faces_Triangles;
float* Faces_Quads;
float* Vertex_Buffer;
float* Normals;

int TotalConnectedTriangles;
int TotalConnectedQuads;
int TotalConnectedPoints;
int TotalFaces;


};



Model_PLY::Model_PLY()
{
// constrator
// initialize
}


float* Model_PLY::calculateNormal( float *coord1, float *coord2, float *coord3 )
{
/* calculate Vector1 and Vector2 */
float va[3], vb[3], vr[3], val;
va[0] = coord1[0] - coord2[0];
va[1] = coord1[1] - coord2[1];
va[2] = coord1[2] - coord2[2];

vb[0] = coord1[0] - coord3[0];
vb[1] = coord1[1] - coord3[1];
vb[2] = coord1[2] - coord3[2];

/* cross product */
vr[0] = va[1] * vb[2] - vb[1] * va[2];
vr[1] = vb[0] * va[2] - va[0] * vb[2];
vr[2] = va[0] * vb[1] - vb[0] * va[1];

/* normalization factor */
val = sqrt( vr[0]*vr[0] + vr[1]*vr[1] + vr[2]*vr[2] );

float norm[3];
norm[0] = vr[0]/val;
norm[1] = vr[1]/val;
norm[2] = vr[2]/val;


return norm;
}



int Model_PLY::Load(char* filename)
{
this->TotalConnectedTriangles = 0;
this->TotalConnectedQuads = 0;
this->TotalConnectedPoints = 0;

char* pch = strstr(filename,".ply");


if (pch != NULL)
{
FILE* file = fopen(filename,"r");

fseek(file,0,SEEK_END);
long fileSize = ftell(file);

try
{
Vertex_Buffer = (float*) malloc (ftell(file));
}
catch (char* )
{
return -1;
}
if (Vertex_Buffer == NULL) return -1;
fseek(file,0,SEEK_SET);

Faces_Triangles = (float*) malloc(fileSize*sizeof(float));
Normals = (float*) malloc(fileSize*sizeof(float));

if (file)
{
int i = 0;
int temp = 0;
int quads_index = 0;
int triangle_index = 0;
int normal_index = 0;
char buffer[1000];


fgets(buffer,300,file); // ply


// READ HEADER
// -----------------

// Find number of vertexes
while ( strncmp( "element vertex", buffer,strlen("element vertex")) != 0 )
{
fgets(buffer,300,file); // format
}
strcpy(buffer, buffer+strlen("element vertex"));
sscanf(buffer,"%i", &this->TotalConnectedPoints);


// Find number of vertexes
fseek(file,0,SEEK_SET);
while ( strncmp( "element face", buffer,strlen("element face")) != 0 )
{
fgets(buffer,300,file); // format
}
strcpy(buffer, buffer+strlen("element face"));
sscanf(buffer,"%i", &this->TotalFaces);


// go to end_header
while ( strncmp( "end_header", buffer,strlen("end_header")) != 0 )
{
fgets(buffer,300,file); // format
}

//----------------------


// read verteces
i =0;
float factor=.1;
for (int iterator = 0; iterator < this->TotalConnectedPoints; iterator++)
{
fgets(buffer,300,file);
sscanf(buffer,"%f %f %f", &Vertex_Buffer[i], &Vertex_Buffer[i+1], &Vertex_Buffer[i+2]);
Vertex_Buffer[i]/=factor;
Vertex_Buffer[i+1]/=factor;
Vertex_Buffer[i+2]/=factor;
i += 3;
}

// read faces
i =0;
for (int iterator = 0; iterator < this->TotalFaces; iterator++)
{
fgets(buffer,300,file);

if (buffer[0] == '3')
{

int vertex1 = 0, vertex2 = 0, vertex3 = 0;
//sscanf(buffer,"%i%i%i\n", vertex1,vertex2,vertex3 );
buffer[0] = ' ';
sscanf(buffer,"%i%i%i", &vertex1,&vertex2,&vertex3 );

Faces_Triangles[triangle_index] = Vertex_Buffer[3*vertex1];
Faces_Triangles[triangle_index+1] = Vertex_Buffer[3*vertex1+1];
Faces_Triangles[triangle_index+2] = Vertex_Buffer[3*vertex1+2];
Faces_Triangles[triangle_index+3] = Vertex_Buffer[3*vertex2];
Faces_Triangles[triangle_index+4] = Vertex_Buffer[3*vertex2+1];
Faces_Triangles[triangle_index+5] = Vertex_Buffer[3*vertex2+2];
Faces_Triangles[triangle_index+6] = Vertex_Buffer[3*vertex3];
Faces_Triangles[triangle_index+7] = Vertex_Buffer[3*vertex3+1];
Faces_Triangles[triangle_index+8] = Vertex_Buffer[3*vertex3+2];

float coord1[3] = { Faces_Triangles[triangle_index], Faces_Triangles[triangle_index+1],Faces_Triangles[triangle_index+2]};
float coord2[3] = {Faces_Triangles[triangle_index+3],Faces_Triangles[triangle_index+4],Faces_Triangles[triangle_index+5]};
float coord3[3] = {Faces_Triangles[triangle_index+6],Faces_Triangles[triangle_index+7],Faces_Triangles[triangle_index+8]};
float *norm = this->calculateNormal( coord1, coord2, coord3 );

Normals[normal_index] = norm[0];
Normals[normal_index+1] = norm[1];
Normals[normal_index+2] = norm[2];
Normals[normal_index+3] = norm[0];
Normals[normal_index+4] = norm[1];
Normals[normal_index+5] = norm[2];
Normals[normal_index+6] = norm[0];
Normals[normal_index+7] = norm[1];
Normals[normal_index+8] = norm[2];

normal_index += 9;

triangle_index += 9;
TotalConnectedTriangles += 3;
}


i += 3;
}


fclose(file);
}

else { printf("File can't be opened\n"); }
} else {
printf("File does not have a .PLY extension. ");
}
return 0;
}

void Model_PLY::Draw(void)
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT, 0,Faces_Triangles);
glNormalPointer(GL_FLOAT, 0, Normals);
glDrawArrays(GL_TRIANGLES, 0, TotalConnectedTriangles);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
first of all ,i dont know what should i do with this code to load a .PLY file to opengl ,on the other hand when i run this code these errors appears:

1>------ Build started: Project: loadPLYtorabi, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\users\sarah\documents\visual studio 2010\Projects\loadPLYtorabi\Debug\loadPLYtorabi.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


please......... help me,i dont know what are these errors and i dont know how to load a .PLY file to opengl
@vlad http://ideone.com/aCmMTs is still having problem. Ideone is also not a good compiler. And thanks for the compiler

@Sarah, please open a new discussion by clicking http://www.cplusplus.com/forum/general/ and then clicking 'new topic' on top right corner. No one will answer your question here..
Last edited on
Pages: 12