May 20, 2019 at 5:25pm UTC
vs2019 gives me lots of errors.
Here is my code...
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
#include <iostream>
#include <vector>
using namespace std;
class funV {
std::vector<int > v;
public :
//default constructor
funV() {
v.push_back(0);
}
//constructor
funV(std::vector<int >& vect) {
v = vect;
}
friend ostream & operator << (ostream &os, const funV &vv);
};
ostream& operator <<(ostream &os, const funV &vv) {
os << "hi" ;
return os;
}
int main() {
vector<int > v1 = { 1,2,3 };
funV one(v1);
cout << one;
return 0;
}
It works in cpp.sh but vs2019 says 16 errors including this...
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'funV' (or there is no acceptable conversion)
Last edited on May 20, 2019 at 5:31pm UTC
May 20, 2019 at 5:31pm UTC
what errors? It works fine in the shell, and it looks fine (well, there is the funky indentation... but it looks correct anyway).
May 20, 2019 at 5:38pm UTC
Works on VS2017 but I don't have VS2019 installed yet.
What happens if you remove line 40, what errors do you get? Might as well post all the errors.
May 20, 2019 at 6:32pm UTC
No errors with VS2019 on my box.
May 21, 2019 at 5:28pm UTC
Sorry guys... My post was misleading. I am doing what you see above but with a header file. So here is my actual code...
header.h
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
#pragma once
#ifndef header_h
#define header_h
#include <iostream>
#include <vector>
class funV {
std::vector<int > v;
public :
//default constructor
funV() {
v.push_back(0);
}
//constructor
funV(std::vector<int >& vect) {
v = vect;
}
friend ostream & operator << (ostream &os, const funV &vv);
int max() {
int m = v[0];
for (int i = 1; i < v.size(); i++) {
m < v[i] ? m = v[i] : m = m;
}
return m;
}
int min() {
int m = v[0];
for (int i = 1; i < v.size(); i++) {
m > v[i] ? m = v[i] : m = m;
}
return m;
}
void say();
};
ostream& operator <<(ostream &os, const funV &vv) {
//os << vv.v[0];
os << "hi" ;
return os;
}
void funV::say() {
for (int i = 0; i < v.size(); i++) {
std::cout << v[i] << " " ;
}
std::cout << std::endl;
};
#endif
main.cpp
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
#include <iostream>
#include <vector>
#include "header.h"
using namespace std;
//a function that takes a vector
int max(vector<int > &vect, int n) {
return 1;
}
int main() {
vector<int > v1 = { 1,2,3 };
funV one(v1);
one.say();
cout << one.max() << " " << one.min();
cout << one;
return 0;
}
Last edited on May 21, 2019 at 5:30pm UTC
May 21, 2019 at 5:53pm UTC
First issue is that you should change header.h line 22 to
friend std:: ostream & operator << (std:: ostream &os, const funV &vv);
Same thing for line 43.
Second issue, less importantly, is that for portability if you ever #include header.h more than once, is to define the functions on line 43 and 51 inside a .cpp file instead of defining it in the header, otherwise you'll get multiple-definitions errors. (Alternatively, declare the functions as inline.).
Last edited on May 21, 2019 at 6:21pm UTC
May 21, 2019 at 6:44pm UTC
Yep, thanks for clarifying that. Requires using the same header in multiple compilation units.
May 22, 2019 at 5:21pm UTC
Thanks Ganado... adding std:: solved my problem.
And yes I will add another .cpp file to put the definitions in. Thank you again....
Here was the errors I was getting...
Severity Code Description Project File Line Suppression State
Error C2065 'os': undeclared identifier Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Warning C4018 '<': signed/unsigned mismatch Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 26
Warning C4018 '<': signed/unsigned mismatch Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 34
Error C2143 syntax error: missing ';' before '&' Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Error C2059 syntax error: 'const' Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Error C2143 syntax error: missing ';' before '{' Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Error C2447 '{': missing function header (old-style formal list?) Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 43
Warning C4018 '<': signed/unsigned mismatch Project1 C:\Users\Quality\Documents\Visual Studio\Project1\Project1\header.h 52