May 7, 2018 at 7:05pm May 7, 2018 at 7:05pm UTC
I am writing a project which contains two template classes
ListManipulator will create lists with etiher int or double
UserInterface will have the output functions and has ListManipulator as a member.
When i try to initialize them, i get the error:
1 2 3
State
Error C2440 'initializing' : cannot convert from 'std::list<float,std::allocator<_Ty>> **' to 'std::list<float,std::allocator<_Ty>> *' listmanipulator.h 18
Error C2439 'ListManipulator<float>::theList' : member could not be initialized listmanipulator.h 18
ListManipulator
1 2 3 4 5 6 7 8
template <typename T>
class ListManipulator {
private :
std::list<T> *theList;
public :
ListManipulator(std::list<T> *aList) : theList(&aList) {}
~ListManipulator() {}
UserInterface
1 2 3 4 5 6 7 8 9 10 11 12 13
template <typename T>
class UserInterface
{
private :
ListManipulator<T> myList;
T minValue;
T maxValue;
bool done;
//std::list<std::unique_ptr<ListManipulator<int>>> myList;
public :
UserInterface(ListManipulator<T> &finalList) : myList(finalList), minValue(0), maxValue(0), done(false ) {}
Main
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
int main() {
bool looping = true ;
while (looping) {
int choice;
std::cout << "Choose type of list." << std::endl
<< "1. Int." << std::endl
<< "2. Double." << std::endl;
std::cin >> choice;
std::cin.get();
if (choice == 1) {
std::list<int > *listan;
ListManipulator<int > finalList(listan);
UserInterface<int > finalUI(finalList);
finalUI.run();
delete listan;
looping = false ;
}
else if (choice == 2) {
std::list<double > *listan;
ListManipulator<double > finalList(listan);
UserInterface<double > finalUI(finalList);
finalUI.run();
delete listan;
looping = false ;
How do i initialize UI the best way?
Last edited on May 7, 2018 at 8:12pm May 7, 2018 at 8:12pm UTC
May 7, 2018 at 8:07pm May 7, 2018 at 8:07pm UTC
That solution gives me:
1 2
Severity Code Description Project File Line Suppression State
Error C2512 'ListManipulator<T>' : no appropriate default constructor available userinterface.h 29
:(
I still used
1 2
ListManipulator(std::list<T> *aList) : theList(&aList) {}
~ListManipulator() {}
since my program will also be able to upload list from a file.
Last edited on May 7, 2018 at 8:08pm May 7, 2018 at 8:08pm UTC
May 7, 2018 at 8:12pm May 7, 2018 at 8:12pm UTC
If it gave you an error, you probably incorporated it into your code incorrectly.
It is strange that you pass a pointer to the list ... why?
And then you initialize theList with the ADDRESS OF THE POINTER? Is that really what you want?
You shouldn't need to use pointers at all here.
May 7, 2018 at 8:40pm May 7, 2018 at 8:40pm UTC
I did doublecheck it and i used it the same way you did, i even removed the pointer.
Now i can not run any UserInterface function from main, giving me LNK2019 error, and i have checked with different function but non work, they are all defined correctly...
May 7, 2018 at 8:47pm May 7, 2018 at 8:47pm UTC
Looks like this
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
#pragma once
#include <list>
#include <random>
#include <ctime>
#include <numeric>
#include <functional>
#include <iterator>
template <typename T>
class ListManipulator {
private :
std::list<T> theList;
public :
ListManipulator(){}
ListManipulator(std::list<T> aList);
~ListManipulator() {}
void fillList();
T sumList() const ;
T listAverage() const ;
bool findFirst1500_1900(T &num) const ;
void divideByTwo();
void swapPlaces();
void findMinMax(T &min, T &max) const ;
void sortList();
void clearList();
std::list<T> getList() const ;
void saveToFile() const ;
void loadFromFile();
};
1 2 3 4 5 6
#include "ListManipulator.h"
template <typename T> ListManipulator<T>::ListManipulator(std::list<T> aList) {
theList(aList);
}
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
#pragma once
// include the appropriate header and define the clearScr and Sleep functions depending on platform used
#if defined __WIN32__ || defined _WIN32
#include <windows.h>
#define clearScr() system("cls")
#else
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
#define clearScr() printf("\033[H\033[J")
#endif
#include "ListManipulator.h"
#include <memory>
#include <list>
template <typename T>
class UserInterface
{
private :
ListManipulator<T> myList;
T minValue;
T maxValue;
bool done;
public :
UserInterface() : minValue(0), maxValue(0), done(false ) {}
~UserInterface(){}
bool chooseList();
void runPro();
void getMinMax();
void fillList();
void menu();
void choice();
};
void goOn(const std::string &message);
void clearScreen();
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
#include "UserInterface.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <list>
using namespace std;
void goOn(const std::string &message)
{
std::cout << message << std::endl;
std::cin.get();
}
void clearScreen() {
clearScr();
}
template <typename T>
void UserInterface<T>::menu(){
std::cout << "##### MENU #####" << std::endl
<< "1. Fill list" << std::endl
<< "2. Summerize list" << std::endl
<< "3. Average value" << std::endl
<< "4. Find first number between 1500-1900" << std::endl
<< "5. Divied by two" << std::endl
<< "6. Swap places" << std::endl
<< "7. Find Min and Max" << std::endl
<< "8. Sort" << std::endl
<< "9. Clear list" << std::endl
<< "10. Write to file" << std::endl
<< "11. Load from file" << std::endl
<< "12. Print list" << std::endl
<< "13. Quit" << std::endl;
}
template <typename T>
void UserInterface<T>::choice()
{
int choice;
std::cin >> choice;
std::cin.get();
switch (choice) {
case 1: fillList();
break ;
case 2: std::cout << "The sum is: " << myList.sumList() << std::endl;
break ;
}
}
void chooseType()
{
}
template <typename T> void UserInterface<T>::runPro()
{
while (done) {
//clearScreen();
//menu();
//choice();
std::cout << "Test" << std::endl;
}
}
template <typename T> void UserInterface<T>::getMinMax()
{
myList.FindMinMax(minValue, maxValue);
std::cout << "Min value is: " << minValue << std::endl
<< "Max value is: " << maxValue << std::endl;
goOn("Press enter for main menu..." );
}
template <typename T> void UserInterface<T>::fillList()
{
clearScreen();
fillList();
std::cout << "The list has been filled with 20 numbers." << std::endl;
goOn("Press enter for main menu..." );
}
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
#include "UserInterface.h"
#include <iostream>
int main() {
bool looping = true ;
while (looping) {
int choice;
std::cout << "Choose type of list." << std::endl
<< "1. Int." << std::endl
<< "2. Double." << std::endl;
std::cin >> choice;
std::cin.get();
if (choice == 1) {
UserInterface<int > finalUI;
finalUI.menu();
looping = false ;
}
else if (choice == 2) {
UserInterface<float > finalUI;
finalUI.menu();
looping = false ;
}
else
std::cout << "Please choose an option between 1 and 2" << std::endl;
}
}
Last edited on May 7, 2018 at 8:49pm May 7, 2018 at 8:49pm UTC
May 7, 2018 at 9:20pm May 7, 2018 at 9:20pm UTC
Can't really understand what it could be. When I dont call for any functions, the program runs fine, but as soon as i initiate finalUI and call for a function, it doesnt compile.
May 7, 2018 at 9:24pm May 7, 2018 at 9:24pm UTC
You aren't handling the template classes properly. The easiest thing for you to do is to put the implementations in the headers. A common way of doing this is to rename your cpp files to tpp (template implementation file) and include it at the end of the associated header file. Remove the includes of the header in the tpp files, of course. When I do that, it works fine.
Last edited on May 7, 2018 at 9:25pm May 7, 2018 at 9:25pm UTC
May 7, 2018 at 9:35pm May 7, 2018 at 9:35pm UTC
So everything in ListManipulator.cpp should be in ListManipulator.h instead?
May 7, 2018 at 9:45pm May 7, 2018 at 9:45pm UTC
Yes. And UserInterface.cpp needs to be in UserInterface.h. But like I said, you can simply include the source code files at the end of the .h files. It's usual to use a tpp suffix for such files. Remove the includes of the headers from the tpp files, though.
Last edited on May 7, 2018 at 9:45pm May 7, 2018 at 9:45pm UTC