C ++ vector <t>其中T是类中的结构

时间:2016-10-25 03:42:07

标签: c++ templates vector

我有一个看起来像这样的课程:

template<typename T>
class MyContainer
{

public:

    struct Handle
    {
        public:

            T* Resolve();
    };

private:

    vector<Handle> mHandles;

};

在我使用时,似乎他们不会为句柄编译迭代器:

vector<Handle>::iterator iter = mHandles.begin();

如果我将其更改为自动,则可以使用:

auto& iter = mHandles.begin();

我不打算明确使用该类型吗?

1 个答案:

答案 0 :(得分:5)

typename是一个从属名称,因此当您在模板函数/类中使用它时,必须在其前面加vector<Handle>::iterator iter = mHandles.begin();

只需更改

typename vector<Handle>::iterator iter = mHandles.begin();

/*
* Anthony Vincenzo Laginess
* CIT 130 HMW 08 Arrays
* 10/19/16
* Time Spent: 
*/
package cit130mhmw08_laginess;

import java.util.Scanner;

public class CIT130MHMW08_Laginess 
{

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);

    System.out.println("------------------------------------------");
    System.out.println("Please enter the total number of dealers: ");
    System.out.println("------------------------------------------");
    int numDealers = input.nextInt();
    numDealers = numberOfDealers(numDealers);
    String[] dealerNames = new String[numDealers];
    double[] dealerSales = new double[numDealers];
    double[] commissionRate = new double[numDealers];

    System.out.println("--------------------------------------------------------");
    System.out.println("Please enter the required data for each of your dealers: %n");
    System.out.println("--------------------------------------------------------");
    dataCalculation(numDealers, dealerNames, dealerSales);

    System.out.println("----------------");
    System.out.println("Dealer Totals: ");
    System.out.println("----------------");
    displayTotals(numDealers, dealerNames, dealerSales, commissionRate);

    System.out.println("----------------");
    System.out.println("Remaining Totals: ");
    System.out.println("----------------");

    System.out.println("");
    System.out.println("Dealership Sales Total: ");
    dealershipSales(numDealers, dealerSales);

    System.out.println("");
    System.out.println("Dealership Sales Averages: ");
    dealerSalesAvg(numDealers, dealerSales);

    System.out.println("");
    System.out.println("Dealership Total Commission: ");
    dealershipTotalCommission(numDealers, commissionRate);

    System.out.println("--------------------------------------------------------");
}//main

//METHOD 1
public static int numberOfDealers(int dealers)
{
    int results;

    Scanner input = new Scanner(System.in);

    while(dealers < 0 || dealers > 30)
    {
        System.out.printf("%nEnter a valid number of dealers: %n");
        dealers = input.nextInt();  
    }
    results = dealers;
    return results;
}//number of dealers methods

//METHOD 2
public static void dataCalculation(int numDealers, String[] dealerNames, double[] dealerSales)
{   
    Scanner input = new Scanner(System.in);

    System.out.printf("%nEnter the names of the dealers:%n ");

        for(int i = 0; i < numDealers; i++)
        {
            String names =input.nextLine();
            dealerNames[i]= names;
        }
    System.out.printf("%nEnter their sales totals: %n");

        for(int i = 0; i < numDealers; i++)
        {
            double sales = input.nextDouble();
            dealerSales[i] = sales;
        }
}//data calculations

//METHOD 3
public static void commission(int numDealers, double[] dealerSales)
{
    //Create array
    double[] commissionRate = new double[numDealers]; 

    for(int i = 0; i < numDealers; i++)
    {
            commissionRate[i] = dealerSales[i];

        if(commissionRate[i] > 0 && commissionRate[i] < 5000)
           commissionRate[i] = commissionRate[i] * 0.08; 
        else if(commissionRate[i] > 5000 && commissionRate[i] < 15000)
            commissionRate[i] = commissionRate[i] * 0.15;
        else if(commissionRate[i] > 15000)
            commissionRate[i] =  commissionRate[i] * 0.20;
    }
    for(int i = 0; i < numDealers; i++)
    {
        System.out.printf("%n$%5.2f" , commissionRate[i]);
        System.out.println("");
    }
}//commission method 

public static void dealershipSales(int numDealers, double[] dealerSales)
{
    //Create array
    double dealershipSalesTotal = 0; 

    for(int i = 0; i < numDealers; i++)
    {
        dealershipSalesTotal += dealerSales[i];  

    }
        System.out.printf("%n$%5.2f" , dealershipSalesTotal);
        System.out.println("");
}//dealership sales

public static void dealerSalesAvg(int numDealers, double[] dealerSales)
{
    double dealerSalesAvgTotal = 0;

    for(int i = 0; i < numDealers; i++)
    {
        dealerSalesAvgTotal += dealerSales[i];
    }
        System.out.printf("%n$%5.2f" , dealerSalesAvgTotal / numDealers);
        System.out.println("");
}//dealership sales averages

public static void dealershipTotalCommission(int numDealers, double[] commissionRate)
{
    double totalCommission = 0;

    for(int i = 0; i < numDealers; i++)
    {
            totalCommission += commissionRate[i] * 100;
    }
        System.out.printf("%n$%5.2f" , totalCommission);
        System.out.println("");
}//total commission for the dealership

public static void displayTotals(int numDealers, String[] dealerNames, double[] dealerSales, double[] commissionRate)
{

    for(int i = 0; i < numDealers; i++)
    {
        System.out.println(dealerNames[i]);
        System.out.printf("$%5.2f" , dealerSales[i]);
        System.out.printf("%n$%5.2f" , commissionRate[i]);
        System.out.println("");
    }
}//display totals  
}//class

会奏效。