Why will my C++ code written in Xcode not compile on other platforms?

closed account (ShqoT05o)
Thank you so much in advanced, but I am looking for more feedback in order not to make the same mistakes again.
I did an assignment that I built on x code. However, when it was checked I received feedback that my code did not run properly and complier found errors, even though it was running on my mine and I did receive an output. Can anyone explain what is wrong with my code?

some of the issues:

error: ‘setprecision’ was not declared in this scope 30 | cout<<setprecision(6) << fixed;
error: ‘sqrt’ was not declared in this scope 45 | float magnitude=sqrt(v.n1*v.n1 +v.n2*v.n2 + v.n3*v.n3); | ^~~~
error: expected unqualified-id before ‘/’ token 105 | / | ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>




using namespace std;

struct Vector
{
    float n1,n2,n3;
};

Vector generate_vector(float a,float b, float c)
{
    struct Vector v;
    v.n1 =a;
    v.n2 =b;
    v.n3 =c;
    return v;
    
}

void show(Vector v)

{
    cout<<setprecision(6) << fixed;
    cout<<"<"<<v.n1<<", "<<v.n2<<", "<<v.n3<<">\n";
    
}

float dot_product(Vector v1, Vector v2)
{
    
    float dot_prod= v1.n1*v2.n1 +v1.n2*v2.n2 + v1.n3*v2.n3;
    return dot_prod;
}

Vector normalize(Vector v)
{
    Vector norm;
    float magnitude=sqrt(v.n1*v.n1 +v.n2*v.n2 + v.n3*v.n3);
    
    norm.n1=v.n1/magnitude;
    norm.n2=v.n2/magnitude;
    norm.n3=v.n3/magnitude;
    return norm;
    
}

Vector const_multiply(float cons, Vector v)
{
    Vector mult;
    mult.n1= v.n1*cons;
    mult.n2= v.n2*cons;
    mult.n3= v.n3*cons;
    return mult;
    
    
}
Vector cross_product(Vector v1, Vector v2)
{
    Vector x_prod;
    x_prod.n1= (v1.n2* v2.n3)-(v1.n3* v2.n2);
    x_prod.n2= (v1.n3* v2.n1)-(v1.n1* v2.n3);
    x_prod.n3= (v1.n1* v2.n2)-( v1.n2* v2.n1);
    return x_prod;

}

Vector add(Vector v1, Vector v2)
{
    Vector add;
    add.n1= v1.n1+v2.n1;
    add.n2=v1.n2+v2.n2;
    add.n3=v1.n3+v2.n3;
    return add;
}

int main()
{
    Vector v1;
    Vector v2;
    float dp;
    
   
    
    v1 = generate_vector(2,1,3);
    v2 = generate_vector(4,-5,8);
    show (v1);
    show (v2);
    dp = dot_product(v1, v2); cout << dp << '\n';
    show(normalize(v1));
    show(const_multiply(5, v1));
    show(cross_product(v1, v2));
    show(add(v1, v2));

    
return 0;
}
    
    
Last edited on
setprecision is in https://www.cplusplus.com/reference/iomanip/
sqrt is in https://www.cplusplus.com/reference/cmath/

Some compilers include iomanip just by including iostream.
Others do not.

You have to know (or look up) all the functions you're using to make sure you include all the required headers.

Yeah, mine (clang++) includes a bunch of headers with <iostream>. It's best to include all of them that you are using to avoid portability issues like what you're getting.
L19 - you don't need to specify struct in C++. Also pass params by const ref rather than by value. As Vect is a simple struct, initialising and returning such a struct can also be simplified. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

struct Vector {
	float n1 {}, n2 {}, n3 {};
};

Vector generate_vector(float a, float b, float c)
{
	 return Vector {a, b, c};
}

void show(const Vector& v)
{
	cout << setprecision(6) << fixed;
	cout << "<" << v.n1 << ", " << v.n2 << ", " << v.n3 << ">\n";
}

float dot_product(const Vector& v1, const Vector& v2)
{
	return v1.n1 * v2.n1 + v1.n2 * v2.n2 + v1.n3 * v2.n3;
}

Vector normalize(const Vector& v)
{
	const auto magnitude {sqrt(v.n1 * v.n1 + v.n2 * v.n2 + v.n3 * v.n3)};

	return Vector {v.n1 / magnitude, v.n2 / magnitude, v.n3 / magnitude};
}

Vector const_multiply(float cons, const Vector& v)
{
	return Vector {v.n1 * cons, v.n2 * cons, v.n3 * cons};
}

Vector cross_product(const Vector& v1, const Vector& v2)
{
	return Vector {(v1.n2 * v2.n3) - (v1.n3 * v2.n2), (v1.n3 * v2.n1) - (v1.n1 * v2.n3), (v1.n1 * v2.n2) - (v1.n2 * v2.n1)};
}

Vector add(const Vector& v1, const Vector& v2)
{
	return Vector {v1.n1 + v2.n1, v1.n2 + v2.n2, v1.n3 + v2.n3};
}

int main()
{
	const auto v1 {generate_vector(2, 1, 3)};
	const auto v2 {generate_vector(4, -5, 8)};

	show(v1);
	show(v2);

	const auto dp {dot_product(v1, v2)};
	cout << dp << '\n';

	show(normalize(v1));
	show(const_multiply(5, v1));
	show(cross_product(v1, v2));
	show(add(v1, v2));
}

it is also recommended to use a double unless you have a hard requirement to use a float.
float and double are *both* promoted to a larger size in the FPU, so it does not save anything except raw byte storage space (and possibly bus activity) to use float, and float's range is too small to do very much outside of classroom work (double barely cuts it, to be honest).

I personally recommend avoiding confusing names as well, you are a capital letter off the built in vector object, making it a little annoying to read and follow (if it were a huge program that used both, it would irritate me to the point of doing a global replace on the name). This is actually a screw up in c++, they should NEVER have named it vector, but here we are.

Not trying to pick on you, but c++ has beautiful operator overloading, making x+y instead of x.add(y) syntax possible, which is much preferred in math code to read it like math instead of like code. Its one of the key features missing in stepchild languages like java. You can steal some other operator for dot product, that is always an aggravation for matrix work. % is an option, weird as that would be (its probably the best pick due to operator precedence oddities from other picks) or leave the oddball ones as text (sigh). When you get away from the basic +-*/ the readability vs handy code issues crop up and you have to decide.
Last edited on
closed account (ShqoT05o)
Thank you all for answering and getting back. I had to comply with rules for the assignment so I do understand the operator overloading it was introduced after the project was turned in. I just now have to argue on why the program ran on my end but not the teaching assistant end and I wanted to understand more on what I was doing wrong as I don’t get that kind of feedback.
its actually pretty good considering where you are in the coursework. The #include confusion (your initial question issue) was not something easy to know, and the rest of it was fine.

jonnin wrote:
This is actually a screw up in c++, they should NEVER have named it vector, but here we are.

Tthat sort of thing bugs me too.

What I do is usually something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#define VArray std::vector

int main ()
{
     VArray <int> numbers {};
     
     VArray.push_back (23);
     
     for (unsigned i = 0; i < numbers.length(); ++i)
     {
          std::cout << numbers.at (i) << " ";
     }
     return 0;
}


It's kind of weird, but it works!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <valarray>
#include <string>
using namespace std;

void display( const string &title, const valarray<double> &V )
{
   cout << title;
   for ( auto e : V ) cout << e << "  ";
   cout << '\n';
}

int main()
{
   valarray<double> v1 = { 2, 1, 3 }, v2 = { 4, -5, 8 };
   display( "v1 = ", v1 );
   display( "v2 = ", v2 );
   cout << "Dot product = " << ( v1 * v2 ).sum() << '\n';
   display( "Cross product = ", v1.cshift(1) * v2.cshift(2) - v1.cshift(2) * v2.cshift(1) );   
   display( "v1 normalised = ", v1 / sqrt( ( v1 * v1 ).sum() ) ); 
   display( "5 v1 = ", 5.0 * v1 ); 
   display( "v1 + v2 = ", v1 + v2 ); 
}


v1 = 2  1  3  
v2 = 4  -5  8  
Dot product = 27
Cross product = 23  -4  -14  
v1 normalised = 0.534522  0.267261  0.801784  
5 v1 = 10  5  15  
v1 + v2 = 6  -4  11  
Last edited on
valarrays are awesome, but a vector has a direction. Its just the wrong word. I get that you can have an abstract idea like a 'column vector' but even that is pushing it as a justification. Or maybe I had too much physics. It still annoys me, though. To be fair, it annoyed me in linear algebra class too.
Last edited on
Topic archived. No new replies allowed.