简体   繁体   中英

Accessing std::vector data members of a class from its member functions

I have a class which defines the following members in a header file (.hpp):

public :
  int loadData(char* filename);

private :
  std::vector<vertex> vertices;
  std::vector<triangle> triangles;
  std::vector<frame> frames;

The vertex , triangle and frame are structures defined in another header file included in this header file.

Now, in the definition of the function loadData in the .cpp, I cannot access the members vertices , triangles and frames . For example, I have the following code resulting in the error shown under it:

cout << "total vertices stored= " << vertices.size() << endl;

motionViewer.cpp:59: error: 'vertices' was not declared in this scope

Why cannot I access these members?

Thank you for the help.

A blind shot: because you forget to put the class-name before the loadData-method in your .cpp:

int className::loadData(char* filename)

But to be sure you need to show more code.

Probably you forgot to use the (class) scope in the cpp file.

Like:

void motionViewer::loadData(char* filename)
{
    // ...
}

Assuming the name of the class is motionViewer .

Are you sure you haven't missed the :: ? In the cpp:

#include "header_name.h"

ClassName::loadData( ..
//...
  1. do your define your structures in another namespace?
  2. have you missed the class scope for the definition of the function?
  3. have you correctly defined your structures (like not missing the tail semicolon) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM