CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2017
    Posts
    1

    Help with Coding (Newbie)

    Hi Guys,

    Need help with this code. Using codeblocks and getting error that the template is not being recognised.

    Code is as follows:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <winddata.h>
    
    void menu(vector<WindData>& windlog);
    void readFile(ifstream& inputFile, vector<WindData>& windlog);
    void averageWind(int monthNum, vector<WindData>& windlog);
    
    int main()
    {
        vector<WindData> windlog;
        ifstream inputFile("MetData-31-3.csv");
    
        if(!inputFile) return -1;
        readFile(inputFile, windlog);
        menu(windlog);
    
        return 0;
    }
    
    void readFile(std::ifstream& inputFile, std::vector<WindData>& windlog)
    {
        float tempSpeed;
        int timeIndex, windSpeedIndex, radiationIndex;
        int columns = 18, field = 0;
        std::string tempDate, tempTime;
        std::string headers[20];
        std::string nStream;
    
        for(int i = 0; i < columns -1; i++)
        {
            getline(inputFile, headers[i], ',');
            if(headers[i] == "WAST")
                timeIndex = i;
    
            if(headers[i] == "S")
                windSpeedIndex = i;
    
            if(headers[i] == "SR")
                radiationIndex = i;
        }
    
        getline(inputFile, headers[17]);
    
        if(inputFile)
        {
            std::string token;
            std::stringstream iss, dateStream;
            while(getline(inputFile, nStream))
            {
                iss << nStream;
                while(getline(iss, token, ','))
                {
                    if(field == timeIndex)
                    {
                        dateStream << token;
                        getline(dateStream, tempDate, ' ');
                        getline(dateStream, tempTime);
                    }
    
                    if(field == windSpeedIndex)
                    {
                        tempSpeed = std::atof(token.c_str());
                    }
    
                    field++;
                }
                field = 0;
                iss.clear();
                dateStream.clear();
    
                WindData windSpeed(tempDate, tempTime, tempSpeed);
                windlog.push_back(windSpeed);
                windSpeed.print();
            }
            std::cout << "Vector Size: " << windlog.size() << std::endl;
        }
    }
    
    void averageWind(int yearNum, std::vector<WindData>& windlog)
    {
        std::string month[12] = {"January", "February", "March", "April", "May", "June",
                                "July", "August" "September", "October", "November", "December"};
    
        int monthCount = 0, monthNum[12] = {0}, monthAverage[12] = {0};
        int dayCount[12] = {0}, totalWindSpeed[12] = {0}, totalRadiation[12] = {0}, mWindAverage[12] = {0};
    
        for (WindData& windData : windlog) {
            if (windData.getYear() == yearNum) {
                int i = windData.getMonth() - 1;
                totalWindSpeed[i] += windData.getSpeed();
                dayCount[i]++;
            }
        }
    
        std::cout << "Wind Speed: " << totalWindSpeed[i] << std::endl;
        std::cout << "Day Count: " << dayCount[i] << std::endl;
    
        for (int i = 0; i < 12; i++) {
            mWindAverate[i] = totalWindSpeed[i] / dayCount[i];
        }
    
        std::cout << mWindAverage[i];
        std::cout << month[i];
    }
    
    void menu(std::vector<WindData>& windlog)
    {
        int option = 0;
    
        while(option != 5)
        {
            std::cout << "Menu:" << std::endl;
            std::cout << "1: \tPrint the maximum wind speed for a specified month & year" << std::endl;
            std::cout << "2: \tPrint the average wind speed for each month of a specified year" << std::endl;
            std::cout << "3: \tPrint the total solar radiation for each month of a specified year" << std::endl;
            std::cout << "4: \tAverage wind speed & solar radiation for each month of a specified year\n\tSaved to file." << std::endl;
            std::cout << "5: \tExit the program.\n\n" << std::endl;
            std::cin >> option;
            switch(option)
            {
                case 1:
                    break;
    
                case 2:
                    int year;
                    std::cout << "Please enter a year" << std::endl;
                    std::cin >> year;
                    averageWind(year,windlog);
                    break;
    
                case 3:
                    break;
    
                case 4:
                    break;
    
                case 5:
                    std::cout << "Exiting..." << std::endl;
                    break;
            }
        }
    }
    winddata.h:

    Code:
    #ifndef WINDDATA_H
    #define WINDDATA_H
    
    using namespace std;
    
    template <class WindData>
    class vector{
    public:
    
        /**default constructor*/
        vector(int size = 100000);
        vector(Vector<WindData>& otherList);
        ~vector();
    
        /**data members*/
    private:
        WindData *list ;
        int length;
        int maxSize;
    };
    
    /** implementation of template class*/
    
    template<class WindData>
    vector<WindData>::vector(int size)
    {
        maxSize = listSize;
        length = 0;
        list = new WindData[maxSize];
    }
    
    template<class WindData>
    vector<WindData>::~vector()
    {
        delete[] list;
    }
    
    /**end of class*/
    #endif
    Last edited by 2kaud; April 23rd, 2017 at 09:45 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Help with Coding (Newbie)

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Help with Coding (Newbie)

    Code:
    template <class WindData>
    WindData is the template type parameter so isn't a defined type as such - just a template parameter. So
    Code:
    void menu(vector<WindData>& windlog);
    is not valid as WindData isn't specified. This should be
    Code:
    template <class WindData>
    void menu(vector<WindData>& windlog);
    and similar for the other function declarations and definitions.

    Also
    Code:
    vector<WindData> windlog;
    WindData type has not been specified. You are going to have to define a type that is passed as the template paramater. Eg
    Code:
    struct mytest {
       int a;
       int b;
    };
    ...
    vector<mytest> windlog;
    or don't use a templated class but define WindData as a non-templated class.

    Why are you defining your own class vector when vector is a STL container?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured