• 13
• 18
• 19
• 27
• 10

# Structure Vector in class

This topic is 2163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am trying to make a class work in C++. This is what I have written so far,

 #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; struct userstats { string ID; int secLev; // 0, normal user (Costumer, can only interact through messages) // 1 moderator (Can delete level 0 users and see list of users) // 2 admin (Same as moderator, but right to delete moderators aswell, defines root password) - INERASABLE string name; string password; }; class userbase { private: string password; public: userbase(); static vector<userstats> users; void setPas(); // void resetdb(string uP); }; userbase::userbase() { password = "default"; } void userbase::setPas() { cout << "Enter current admin/root password: "; string pass; getline(pass); if (pass == userbase::password) { password = pass; userbase::users[1].password = pass; } else cout << "Password incorrect."; } int main() { userbase current; ifstream inFile; inFile.open("userlist.txt"); if(!inFile.good()) { current::users.push_back(); ofstream outFile("userlist.txt"); outFile << current::users(1).ID; } return 0; } 

This gives me a couple of errors when I try to build it,

||=== brugersystem v2, Debug ===|
Line 36 |error: no matching function for call to 'getline(std::string&)'|
Line 59 |error: 'current' is not a class or namespace|
Line 61 |error: 'current' is not a class or namespace|
||=== Build finished: 3 errors, 0 warnings ===|

I am not sure with any of them. The line 59 and 61 errors are given when I want to create a database with a "default" admin member, Line 36 I try to recieve line input from a user. This whole program is to be a sort of vague usersystem. It will check whether the .txt file containing information exists. If not, it should create the database with an default admin account.

Regards,
Boooke

##### Share on other sites

||=== brugersystem v2, Debug ===|
Line 36 |error: no matching function for call to 'getline(std::string&)

Well first off you probably want: "getline(cin, pass);"

Line 59 |error: 'current' is not a class or namespace|
Line 61 |error: 'current' is not a class or namespace|

Secondly you're trying to access an object with the wrong operator, try "."

As in "current.users.push_back();"

Without the quotes obviously. Keep in mind you aren't actually pushing anything back either, so, that won't do much. Oh and you should probably use an enum for seclevel.

Bleh, okay, there's actually a lot of things wrong with this whole thing.

EDIT: I "partially" rewrote it for you, I honestly couldn't figure out what you were trying to do on half of it and I couldn't figure out what setPas was supposed to be doing so I just removed it.

 #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; enum SecurityLevel { NORMAL, MODERATOR, ADMIN }; struct User { string ID; string name; string password; SecurityLevel secLevel; }; class Database { public: Database(); Database(string rootPass); vector<User> userTable; private: string m_rootPass; }; Database::Database() { m_rootPass = "admin"; } Database::Database(string rootPass) { m_rootPass = rootPass; } int main() { Database database; ifstream file("userlist.txt"); string line; if(file.is_open()) { while(file.good()) { getline(file, line); // Do some tokenizing and assignment here } } file.close(); return 0; } 

##### Share on other sites
Hey, thank you, and sorry for the late answer. Yes, I know, a mess. I did use enums before, but I thought i'd optimize everything when it was all clear and functional. I was thinking, if it is allowed and somewhat accepted to make checks inside the class' constructor even before creating an class object, like this:

 Userbase::Userbase() { ifstream inFile("userlist.txt"); if (inFile.good()) { // Read password from first user (admin) in .txt } else rootPass = "Admin1"; inFile.close(); } 

Also, I seem not to be able to use push_back() with this,

 int main() { Userbase userbase; ifstream inFile; inFile.open("userlist.txt"); if(inFile.good()) { // ADD DATE OF MODIFICATION cout << "USERLIST FOUND, READING USERS.\n"; inFile.close(); } else { cout << "NO USERS FOUND, CREATING NEW LIST.\n"; inFile.close(); userbase.users.push_back(1, "admin", "Admin1", admin); ofstream outFile("userlist.txt"); time_t current = time(0); outFile << "created: " << current << "\n"; outFile << "mod_date: " << current << "\n\n"; outFile.close(); } return 0; } 

It gives me the error at line 78 (Which is the push_back() command),

error: no matching function for call to 'std::vector<userstats, std::allocator<userstats> >::push_back(int, const char [6], const char [7], securityLevel)'|

The vector structure looks like this,

 struct userstats { string ID; string name; string password; securityLevel secLev; // enum securityLevel {user, moderator, admin}; }; 

Btw, all the functions I have written (SetPas(), resetdb()) in the class are ideas that I have written down to remember and fully create when I have sorted this vector thing out. I have not fully written them.

##### Share on other sites
ID is a string, not an int.

##### Share on other sites
... Whops! Thanks for the heads up. but the problem seem to persist with the same error except for the parameter in the error "int" changing to "const char[3]".

##### Share on other sites
push_back() wants a copy of the object to be put into the container, not the components that make up the object. Give your struct a constructor and pass the relevant information to the constructor in the push_back() call.

##### Share on other sites
Ah, I see. So everytime I want to create a new element in the vector I will have to copy a whole structure object to the vector? I wrote something like this,

 ... else { cout << "NO USERS FOUND, CREATING NEW LIST.\n"; inFile.close(); userstats buffer = {"d1", "admin", "Admin1", admin}; userbase.users.push_back(buffer); ofstream outFile("userlist.txt"); time_t current = time(0); outFile << "created: " << current << "\n"; outFile << "mod_date: " << current << "\n\n"; outFile.close(); } ... 

Do you know the reason for this? A security measure for the correct data to be parsed, maybe? And thank you both for the solutions.

Another note: I do not recommend putting using namespace std; on top like that. You are putting all that junk into the global namespace, which isn't good practice. I'd recommend putting it inside functions instead, or at least, only do stuff like this:
 using std::cout; using std::cin; using std::string;