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!!

1
  • A nice example why you should make a habit of making all source and comments English; one day someone not knowing your native language WILL look at your code, making things awkward for both sides. -- Ein schönes Beispiel warum man sich zur Angewohnheit machen sollte, allen Code und Kommentare in Englisch zu halten; eines Tages WIRD jemand deinen Code lesen müssen, der deiner Muttersprache nicht mächtig ist, mit Problemen für beide Seiten. Commented Apr 22, 2013 at 15:51

4 Answers 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.

Sign up to request clarification or add additional context in comments.

2 Comments

It can be called on an instance, too, although it makes little sense to do so in this case.
exactly, so second part of answer is not valid
1

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

Comments

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)

2 Comments

He should only pass it by pointer if he intends that they are optional. Also he should use const references as he appears to only be reading the values in the vector.
Yes, but If he is obviosly very early beginner, there is no point to go for const at this time and solve, why it is useful. With pointer passing, you are right.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.