Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

any idea? #include<iostream> using std::cout; using std::endl; using std::ostrea

ID: 3779892 • Letter: A

Question

any idea?

#include<iostream>
using std::cout; using std::endl;
using std::ostream; using std::cin; using std::boolalpha;
#include<algorithm>
using std::copy; using std::sort;
#include<utility>
using std::swap;
#include<string>
using std::string;
#include<vector>
using std::vector;
#include<iterator>
using std::distance; using std::istream_iterator;
using std::back_inserter;
#include<sstream>
using std::ostringstream;

// ADD MORE INCLUDES if you need them to the above

template<typename T>
struct SetElement{
public:
T element;
int cnt;
SetElement()=default;
SetElement(T val);
};

// FIX BELOW for SetElement!!!
// The T val constructor and operator<< below here for SetElement
template<typename T>
SetElement<T>::SetElement(T val){
cnt = 1;
element = val;
}

template<typename T>
ostream &operator<<(ostream os, const SetElement<T>& p){
os<<p.element;
return os;
}


template <typename T>
class MSet{
private:
SetElement<T>* ary_;
size_t size_;
size_t capacity_;
void grow();
  
public:
MSet(size_t s=2);
MSet(T val);
MSet(vector<T>&);
MSet(MSet&);
MSet operator=(MSet);
~MSet();
size_t size();
size_t capacity();
SetElement<T>* find(T);
void insert(T);
size_t count(T val);
bool erase(T);

friend ostream& operator<<(ostream& out, MSet<T>& m){
// FIX THIS RIGHT HERE. ENTER YOUR CODE HERE
out<<"test";
return out;
};
};

// MANY THINGS BELOW HERE FOR MSet
template <typename T>
MSet<T>::MSet(size_t s){
  
ary_ = new T[s]();
  
}
/*
template <typename T>
size_t MSet<T>::size_(){
cout <<"test"<< endl;
return size_t;
}
*/

// DO NOT CHANGE ANYTHING BELOW. TESTING!
int main(){

int test;
long l1, l2, l3;

cin >> test;
cout << boolalpha;

switch (test){

// basic constructors, .size() and .capacity()
case 1 : {
MSet<long> m_l;
/*
cout << m_l.size() <<":"<< m_l.capacity()<<endl;
cin >> l1;
MSet<long> m_l_oneval(l1);
cout << m_l_oneval.size() <<":"<< m_l_oneval.capacity()<<endl;
break;
*/}

}

Compilation failed. solution .cc In instantiation of MSet

Explanation / Answer

your mistack is:

ary_ = new SetElement<T>[s]; is correct.

not ary_ = new T[s]();

Program:

#include<iostream>
using std::cout; using std::endl;
using std::ostream; using std::cin; using std::boolalpha;
#include<algorithm>
using std::copy; using std::sort;
#include<utility>
using std::swap;
#include<string>
using std::string;
#include<vector>
using std::vector;
#include<iterator>
using std::distance; using std::istream_iterator;
using std::back_inserter;
#include<sstream>
using std::ostringstream;
// ADD MORE INCLUDES if you need them to the above
template<typename T>
struct SetElement{
public:
T element;
int cnt;
SetElement()=default;
SetElement(T val);
};
// FIX BELOW for SetElement!!!
// The T val constructor and operator<< below here for SetElement
template<typename T>
SetElement<T>::SetElement(T val){
cnt = 1;
element = val;
}
template<typename T>
ostream &operator<<(ostream os, const SetElement<T>& p){
os<<p.element;
return os;
}

template <typename T>
class MSet{
private:
SetElement<T>* ary_;
size_t size_;
size_t capacity_;
void grow();
  
public:
MSet(size_t s=2);
MSet(T val);
MSet(vector<T>&);
MSet(MSet&);
MSet operator=(MSet);
~MSet();
size_t size();
size_t capacity();
SetElement<T>* find(T);
void insert(T);
size_t count(T val);
bool erase(T);
friend ostream& operator<<(ostream& out, MSet<T>& m){
// FIX THIS RIGHT HERE. ENTER YOUR CODE HERE
out<<"test";
return out;
};
};
// MANY THINGS BELOW HERE FOR MSet
template <typename T>
MSet<T>::MSet(size_t s){
  
ary_ = new SetElement<T>[s];
  
}
/*
template <typename T>
size_t MSet<T>::size_(){
cout <<"test"<< endl;
return size_t;
}
*/
// DO NOT CHANGE ANYTHING BELOW. TESTING!
int main(){
int test;
long l1, l2, l3;
cin >> test;
cout << boolalpha;
switch (test){
// basic constructors, .size() and .capacity()
case 1 : {
MSet<long> m_l;
/*
cout << m_l.size() <<":"<< m_l.capacity()<<endl;
cin >> l1;
MSet<long> m_l_oneval(l1);
cout << m_l_oneval.size() <<":"<< m_l_oneval.capacity()<<endl;
break;
*/}
}
}