-2

I get the no default constructor exists for class "vector" Error

main.cpp

#include <iostream>
#include <string>
#include "myVector.h"

using namespace std;

int main()
{   
    vector<int> myvecA;
    cout << "Vector A size: " << myvecA.size() << endl;

system("Pause");
return 0;
}

myVector.h

#include <iostream>
 using namespace std;


 template <class V>
 class vector{

public:
 vector(V x) {
    bool(x = 0)
        sizearr = 0;
    if (x != 0)
        return x;
    sizearr = x;

}

  V size();

private:

  V sizearr;

};

template <class V>

 V vector<V>::size() {
     return sizearr;
}

I don't know how to make it so that if

vector<int> myvecA;

doesn't contain a construct that it will set the construct to 0 so i can return size as 0.

I apologize if my question is not very clear. I'm just look for help so I can learn.I'm not very good at c++.

J.g
  • 11
  • 2
  • 1
    There is already `std::vector1`. The use [of the obnoxious "using namespace std"](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and then declaring one's own `vector` just makes things confusing. You need to completely forget that "using namespace std;" exists in C++. It's bad programming practice. – Sam Varshavchik Oct 23 '16 at 01:15
  • Wait, you're declaring your own `vector` class and you don't provide a default constructor? Just what the error message says? Why not simply use the `std::vector` BTW? – πάντα ῥεῖ Oct 23 '16 at 01:17
  • 2
    `bool(x = 0) sizearr = 0;` is a syntax error. Your compilershould say something – M.M Oct 23 '16 at 01:30
  • 1
    Throw a handful of darts blindfolded, some will stick. You are templating without a fundamental understanding of classes. This is a very hard way to learn. – lakeweb Oct 23 '16 at 01:35
  • @M.M That depends on the compiler he's using. – American Patriot Oct 23 '16 at 01:42
  • 1
    @AmericanPatriot Trump's compiler is the only reliable one. Narrow enough to reflect their worlds view. Something like Turbo C++. – πάντα ῥεῖ Oct 23 '16 at 01:45

1 Answers1

0

You just need to provide a default constructor for the class

template <class V> class vector {

public:
 explicit vector(): sizearr(0) {}
}

Also, constructors do not "return" in the sense used for normal functions. A constructor initializes the internals of a class, the memory needed for the object of the class is created using either new keyword or allocated on the stack.


One more thing, you have to do is to prevent your class from interfering with existing classes (in this case std::vector). The way to do this is to remove that declaration of using namespace std from your header file. Having using namespace std will become a problem if you decide to include your header in a file that uses the standard std::vector in which case your new class will conflict with the already existing one or vice versa.

It is also good practice not to use using namespace X declarative because namespaces safeguard against naming conflicts, and exposing the names in the namespace is in direct conflict with the purpose of a namespace. The only recommended place to use using namespace X is when you are implementing functionality for a given namespace in a "private" .cpp file.

Hope that helps. Ask more questions

smac89
  • 39,374
  • 15
  • 132
  • 179