arg sntax problems and whatnot.

Started by
9 comments, last by snk_kid 19 years, 8 months ago
Ok, Im very new to c++ a week maybe.. Also i live in eastern tennesee and im 15 so theres not really anyone I can ask about this stuff. What im trying to accomplis is a login/logout program that puts stuff in a file... so im trying to uses as many things that ive learned as possible in this. First off... This wont compile. It gives me errors but I dont know what im doing wrong. So if you could help me figuer out what im doing wrong and also if there seems to be something I don't seem to fully understand could you help me with it. #include <iostream> #inlcude <string> #inlcude <fstream> class user{ public: string GetName() {return usrname;} void SetName(usr) {usrname = usr;} int GetAge() {return userage;) void SetAge(usra) {userage = usra;} string GetUsrName() {return usersn;} void SetUsrName(usrsn) (usersn = usrsn;} string GetUsrPass() {return userpass;} void SetUsrPass(usrp) {userpass = usrp;} private: string & usrname; int & userage; string & usersn; string & userpass; }; int main() { return 0; } Ill be posting other code here as to not clog up the forums.
[href]http://neoeden.web1000.com[/href]
Advertisement
Uhm, well you are declaring your private members as references, which cannot work. You also seem to miss the std:: before your strings (or you forgot using namespace std;)
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
some of that helped.... but its still buggy
[href]http://neoeden.web1000.com[/href]
Quote:Original post by xMcBaiNx
Uhm, well you are declaring your private members as references,


It can, just probably not the way he intendend.

cyberflame: you're declaring your variables AFTER you use them. Try doing your variable declerations FIRST. We should be looking at:

#include <iostream>#inlcude <string>#inlcude <fstream>using namespace std;class user{private:    string  usrname;    int userage;    string usersn;    string userpass;public:    string GetName() {return usrname;}    void SetName(usr) {usrname = usr;}    int GetAge() {return userage;)    void SetAge(usra) {userage = usra;}    string GetUsrName() {return usersn;}    void SetUsrName(usrsn) (usersn = usrsn;}    string GetUsrPass() {return userpass;}    void SetUsrPass(usrp) {userpass = usrp;}};int main(){    return 0;}


I should also note that using Get/Set functions are generally considered bad design in cases where the variables in question should just be public. This is one of those places where I think just making the variable public would be much saner.

Also, you should ALLWAYS post your error messages. Copy and paste. Just because you don't know what they mean dosn't mean we don't ;-).
MaulingMonkey: That doesn't matter in a class. Almost every class I've written has the public interface before the private data in the file. If the data were global, that would different.

-Auron
lets see . . there are several things that you need to understand. heres a simplified version of your class, the way it should be.
for the record - classes can be used the way you have it, without any constructors or destructors, but in that case use a struct instead.
class user
{
public:
/* you have no constructor or destructor, which means that you won't be able to create a default object of this class so ...*/
/* this is a default constructor - it allows you to create an instance of the class by simply saying - Hack hacker; - and leaving it undefined. it is a function, so it must have a definition it in your .cpp file or in the .h file. you can use it to determine how your private member variables are initialized on startup. since nothing really needs to be done, leave it blank */
user() { }
/* a destructor - it is also a function, and defines what happens to your class when it goes out of scope. since there are no pointers, it doesn't need to do anything */
~user() { }
/* retreive the password */
std::string GetUsrPass( void ) { return usrpass; }
/* here, you have to add the data type to the set function parameter list, otherwise the compiler doesn't know what usrp is. so instead of void SetUsrPass( usrp ) you'd have */
void SetUsrPass( std::string usrp ) { userpass = usrp; }
private:
std::string usrpass;
};
Quote:cyberflame: you're declaring your variables AFTER you use them

this really makes no difference, the compiler sorts it out.
hope that helps.

<edit> killed the source tags
- stormrunner
Okay.. Here it is again.. Ive made all the changes you all suggested and included my compile log. Im wondering tho. How i would use this with references (and pointers if you could show me)Also what can i and an i not put into an #include "file" Is there another command too? what is the scope of references that have been assigned a value? Did it just make the value global oris the reference just global and the original dissapears everytime it goes out of scope? (hope that made sense)

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class user{
public:
void user() {}
void ~user() {}
std::string GetName() {return usrname;}
void SetName(std::string usr) {usrname = usr;}
std::int GetAge() {return userage;)
void SetAge(std::int usra) {userage = usra;}
std::string GetUsrName() {return usersn;}
void SetUsrName(std::string usrsn) (usersn = usrsn;}
std::string GetUsrPass() {return userpass;}
void SetUsrPass(std::string usrp) {userpass = usrp;}
private:
string usrname;
int userage;
string usersn;
string userpass;
};

int main()
{
return 0;
}




Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Program Files\Blood Shed\Dev-Cpp\New Folder\New Folder\Untitled3.cpp" -o "C:\Program Files\Blood Shed\Dev-Cpp\New Folder\New Folder\Untitled3.exe" -I"C:\Program Files\Blood Shed\Dev-Cpp\include\c++" -I"C:\Program Files\Blood Shed\Dev-Cpp\include\c++\mingw32" -I"C:\Program Files\Blood Shed\Dev-Cpp\include\c++\backward" -I"C:\Program Files\Blood Shed\Dev-Cpp\include" -L"C:\Program Files\Blood Shed\Dev-Cpp\lib"
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:7: return
type specification for constructor invalid
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:8: return
type specification for destructor invalid
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:11: parse
error before `int'

C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:13: semicolon
missing after declaration of `user'

C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp: In
member function `std::string user::GetName()':
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:9: `
usrname' undeclared (first use this function)
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:9: (Each
undeclared identifier is reported only once for each function it appears
in.)
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp: At
global scope:
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:13: ISO
C++ forbids defining types within return type
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:13: `
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:13: sorry,
not implemented: `parm_decl' not supported by dump_type
<type error>' is not an aggregate type
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:13: syntax
error before `(' token
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:14: `
usersn' was not declared in this scope
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:14: `

usrsn' was not declared in this scope
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:14: parse
error before `;' token
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:16: `
SetUsrName' declared as function returning a function
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp: In
function `int SetUsrName(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)':
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:16: `
userpass' undeclared (first use this function)
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:16: `
usrp' undeclared (first use this function)

C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp: At
global scope:

C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:17: parse
error before `private'
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:21: `
std::string userpass' used prior to declaration
C:/Program Files/Blood Shed/Dev-Cpp/New Folder/New Folder/Untitled3.cpp:22: parse
error before `}' token

Execution terminated
[href]http://neoeden.web1000.com[/href]
#include <iostream>#include <string>#include <fstream>class user {   std::string usrname;   std::string usersn;   std::string userpass;   int userage;   public:   //default constructor   user(int userAge = 0,        const std::string& username = std::string(),        const std::string& usersurname = std::string(),        const std::string& userpassword = std::string())   : usrname(username), usersn(usersurname), userpass(userpassword), userage(userAge) {}   const std::string& getName() const { return usrname; }   void setName(const std::string& usr) { usrname = usr; }   int getAge() const { return userage; }   void setAge(int usra) { userage = usra; }   const std::string& getUsrName() const { return usersn; }   void setUsrName(const std::string& usrsn) { usersn = usrsn; }   const std::string& getUsrPass() const { return userpass; }      void setUsrPass(const std::string& usrp) { userpass = usrp; }};int main() {   user u;   user u2(23, "Jim", "bob", "scooby");   std::cout << u2.getName() << '\n';   return 0;}
Quote:
/* you have no constructor or destructor, which means that you won't be able to create a default object of this class so ...*/
/* this is a default constructor - it allows you to create an instance of the class by simply saying - Hack hacker; - and leaving it undefined. it is a function, so it must have a definition it in your .cpp file or in the .h file. you can use it to determine how your private member variables are initialized on startup. since nothing really needs to be done, leave it blank */
user() { }
/* a destructor - it is also a function, and defines what happens to your class when it goes out of scope. since there are no pointers, it doesn't need to do anything */
~user() { }


This is wrong. If you do not supply a constructor, destructor, copy constructor, or assignment operator, the compiler will provide one for you, though it may not do what you want. The compiler-provided assignment operator, for instance, just does a straight bit copy of your data members. It's still good to create a default constructor but you don't have to, for this class.
@ snk_kid
Could you explain this better? Ok first thing i dont get is that you didn't actually declare this part private
std::string usrname;
std::string usersn;
std::string userpass;

int userage;
Daes that mean its implied or did you forget.. (sry im learning so im gonna scan this like an english teacher =P)

Ok next is this
//default constructor
user(int userAge = 0,
const std::string& username = std::string(),
const std::string& usersurname = std::string(),
const std::string& userpassword = std::string())
: usrname(username), usersn(usersurname),
Whats the single colon for? is it does it do something im not aware of? Also im kind of confused about the whole whant to use standard and not to use standard thing. = And is there actually any advantage to using references? (i need to know because im assuming classes are global, but i may be wrong. and is value(differentvalue) the same as value = different value?


#include <iostream>
#include <string>
#include <fstream>

class user {

std::string usrname;
std::string usersn;
std::string userpass;

int userage;

public:

//default constructor
user(int userAge = 0,
const std::string& username = std::string(),
const std::string& usersurname = std::string(),
const std::string& userpassword = std::string())
: usrname(username), usersn(usersurname), userpass(userpassword), userage(userAge) {}

const std::string& getName() const { return usrname; }

void setName(const std::string& usr) { usrname = usr; }

int getAge() const { return userage; }

void setAge(int usra) { userage = usra; }

const std::string& getUsrName() const { return usersn; }

void setUsrName(const std::string& usrsn) { usersn = usrsn; }

const std::string& getUsrPass() const { return userpass; }

void setUsrPass(const std::string& usrp) { userpass = usrp; }
};

int main() {

user u;

user u2(23, "Jim", "bob", "scooby");

std::cout << u2.getName() << '\n';

return 0;
}
[href]http://neoeden.web1000.com[/href]

This topic is closed to new replies.

Advertisement