user3168609
user3168609

Reputation:

Exact same C++ Code works in Visual Studio but not in Xcode

This is an assignment for C++ class, it compiles and runs perfectly in Visual Studio 2013 on Windows, but I mainly code on OSX in Vim or Xcode.
XCode immediately shows me the error "no matching constructor for initialization of 'Vector'" in the line, same when I try to compile with a Makefile and the terminal.

    Vektor C = A + B; // !!! no matching constructor for initialization of 'Vector'

however,

    Vektor B = A;
    A + B;

work perfectly on both OSX and Windows.
I did not try to export/import project files, it's completely new files in every environment.

The complete code: http://pastebin.com/xabY0w08

Is that a known problem, if so, why does it happen and is there any way to make it work?

Upvotes: 2

Views: 1019

Answers (3)

marcinj
marcinj

Reputation: 49986

You must change:

Vektor(Vektor &quelle);

to

Vektor(const Vektor &quelle);

the reason is that temporary object (A+B) cannot bind to a non-const reference,

Upvotes: 4

OlivierH
OlivierH

Reputation: 347

in

Vektor C=A+B;

The operator+ returns a Vektor - value, but your constructor accepts a Vektor& - reference.

You need to implements a contructor which receive a const Vektor& (just modify your existing one).

It works on windows because (not sure about that) Visual C++ is more lenient about const references.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

Define the copy constructor as having const reference to an object of the class. For example

Vektor( const Vektor & );

Upvotes: 6

Related Questions