i'm trying doing a static clas, but i, always get errors... now i have head pain:(

class Pessoa2
{
private:
    static string strnome;
public:
    Pessoa2()
    {
        strnome="ana";
    }

    void setnome(string value)
    {
        strnome=value;
    }
    static string getnome()
    {
       return strnome;
    }
};

just seen these static class, what i'm doing wrong?:(

Recommended Answers

All 20 Replies

what are the errors? line 4 doesn't make the whole class static, just the object declared there.

The static keyword means that there will be only one instance of the object no matter how many instances there are of the class. When you declare an object static then you also have to declare it globally in a *.cpp file. Assuming the class you posted in in a file "class.h"

// main.cpp

#include "class.h"

string  Pessoa2::strnome;

Also, make sure to include <string> header file.

let me ask you something:
can i avoid these line:
string Pessoa2::strnome;
and put it in construtor?

better question: how i can test if 1 variable was inicializated?
(i tryied: "if(nome==NULL)" but i get errors:()

@cam - No, this has to be outside of the class definition.
You're delcaring a static reference, the class has to know where it is initialised.

(i tryied: "if(nome==NULL)" but i get errors:()

Assuming nome is std::string, you get an error because std::string cannot be compared to NULL. Here are a few ways to check for empty string

if(nome.length() == 0)

or
if(nome.size() == 0)

or
if(nome == "")

static void setNome(string strnome)
    {
        if(nome.length() == 0) string nome="ana";
        nome=strnome;
           showname();

    }

error message:
"C:\Users\Joaquim\Documents\CodeBlocks\test2\main.cpp|26|undefined reference to `Pessoa2::nome'|"

Why did you declare nome inside the if statement?

I think you have the variables mixed up. strnome is a member of Pessoa2 class. You need to pay closer attention to what is and is not class members.

static void setNome(string nome)
    {

        strnome = nome;
        if(strnome.length() == 0) 
           strnome="ana";
        showname();

    }

i belive these is a compiler error:(

static void setNome(string nome)
    {

        if(strnome.length() == 0)
           strnome="ana";
        strnome = nome;//error
        showname();
    }

error message:
"C:\Users\Joaquim\Documents\CodeBlocks\test2\main.cpp|27|undefined reference to `Pessoa2::strnome'|"
i don't understand these error... seems the compiler ignores the 'if':(
these don't make sence:(

why if i do these:

class Pessoa2 //static class
{
    string strnome=""; //error
public:
.............

i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\test2\main.cpp|8|error: invalid use of member 'Pessoa2::strnome' in static member function|"
????

data cannot be initialized when declared. Besides, the default for std::string is "" (empty string), so no need to initialize it yourself anyway.

class Pessoa2 //static class
{
    string strnome;
public:
.............

but the static functions don't accept the normal variables... only static:(
more i hate C++:(
so many evolutions and don't use properties(at least the namespace can help us on these) and events(only Visual Studio, but isn't portable:()
hey... properties and events are OOPL concepts:(

more i hate C++:(

It's a learning curve that's getting you. Stick in there and you'll get it all figured out, it just takes some time and practice.

but the static functions don't accept the normal variables

Yes, that's by design. How can a static function know the value of an instance variable? If there are 10 instances which one should it use?

and don't use properties

What do you think class objects are? In the example you posted strnome is a property of Pessoa2 class.

and events(only Visual Studio, but isn't portable:()

events are not part of the c++ language but rather an extension of the c++ language much like any other library. You get similar events with other compilers such as Borland, QT, and libraries such as wxWidgets. Don't confuse the language which is defined by c++ ISO standards committee with what is in libraries which anyone can write.

sorry, when i said properties i mean these format:
object.property=value
;)
and yes i will figure out;)
with templates we can do several things. i have the property code and i update it;)
(see my topics;))
sorry if i bored you and thanks for all

Normally, you initialize static class variables in a translation unit (a source file, NOT a header). IE, in pessoa2.cpp:

#include "./pessoa2.h"

string Pessoa2::strnome("ana");
.
.
.

You should NOT be initializing it in the header (class definition), although I think the latest C++ standard does allow that (not 100% sure) - most compilers will not be so current. :-)

that's why someone tell me for learn C# lol
thanks for all... thanks

that's why someone tell me for learn C# lol

C# has its own quirks that complicate the learning curve, so don't expect any magic by learning a different language. ;)

that's why i love my new language that i will do it;)

heres a nice way for build 1 class 100% 'static':

class classname
{
    //class menbers... normal way(don't use static, because it's more easy)
}classname;

now you don't need a object for use the class. but if you need a object just change the last line:

class classname
    {
        //class menbers... normal way(don't use static, because it's more easy)
    }classname, object1, object2, objectn;

now the class can be used with it's name and it's objects...
(now i did 1 test)
when you declare a class objects, you can use the name too:

person2 person2,f;

cool;)

heres a nice way for build 1 class 100% 'static'

The "static" object must be in scope wherever you use it, which essentially means it must be global (usually not a good idea). Further, you cannot access "static" members through anything except the "static" object. Other objects will have their own copies of the "static" members, which completely defeats the purpose.

I wouldn't recommend that solution in general, and I certainly wouldn't describe it as a static class.

the:

person2 person2,f;

it's incorrect sorry about that:(
i can't use 'static' members because i need inicializate the variables outside why i want that type of class?
because of 2 things:
1 - if i need use a class without objects... these is the best way;
2 - imagine with virtual functions, you can do a new class diretive of the class base. so i can use the new class without objects.(is for simulate events).
but thanks for your advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.