1

I am writing a code to work with vectors in C++. I've got 3 files: main.cpp, Vektor.cpp and Vektor.h Now I want to call a static funktion in main, which is implemented in Vektor.cpp and declarated in Vektor.h. "test" and "test2" are two instances of the class Vektor. Eclipse throws an Error out, but i do not know why; it says

Multiple markers at this line - Function 'addieren' could not be resolved - 'addieren' was not declared in this scope - Invalid overload of 'endl' - Line breakpoint: main.cpp [line: 28]

Where is the mistake? The "Vektor.h" is included. Here are the necessary cuttings:

main.cpp:

// ...
cout << "Summe: " << addieren(test,test2) << endl;

Vektor.cpp:

Vektor Vektor::addieren(Vektor vektor1, Vektor vektor2)
{
Vektor vektorSumme;
vektorSumme.set_x(vektor1.get_x() + vektor2.get_x());
vektorSumme.set_y(vektor1.get_y() + vektor2.get_y());
vektorSumme.set_z(vektor1.get_z() + vektor2.get_z());
return vektorSumme;
} 

Vektor.h:

class Vektor

{
//...
public:
  //...
static Vektor addieren(Vektor vektor1, Vektor vektor2);

Thanks for helping!!

4

4 回答 4

5

You need to call it as:

Vektor::addieren(test,test2);

static member functions can be called with fully qualified name of the class. They can also be called on a class instance, but since you don't have any instance it doesn't apply here.

于 2013-04-22T15:41:55.350 回答
1

The syntax for calling static functions is: Vektor::addieren(...)

于 2013-04-22T15:42:27.573 回答
1

You should call it

Vektor::addieren(test, test2)

But I would suggest you, to improve addieren function to pass both vectors by reference or pointer.

addieren(Vektor & vektor1, Vektor & vektor2)

or

addieren(Vektor * vektor1, Vektor * vektor2)

but then you must call it with

Vektor::addierent(&test, &test2)
于 2013-04-22T15:44:41.397 回答
1

You need to call this with fully qualified name of the class, as:

Vektor v_res=Vektor::addieren(test, test2);

or on an object (instance of class):

Vektor v;
Vektor v_res=v.addieren(test, test2);
于 2013-04-22T16:08:54.317 回答