错误:未在此范围内声明朋友功能

时间:2020-05-07 20:37:18

标签: c++

/ **************** pair.h ******************** /

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class Utilities {

    private HashMap<String, Integer> myMap;

    public Utilities() {
        myMap = new HashMap<String, Integer>();
    }

    public void populateHashMap() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the words separated by space: ");
        String line = input.nextLine();
        for (String next : line.split("\\s+")) {
            myMap.put(next, myMap.getOrDefault(next, 0) + 1);
        }
    }

    public void displayCollection() {
        for (Entry<String, Integer> entry : myMap.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }

    public static void main(String[] args) {
        Utilities demo = new Utilities();
        demo.populateHashMap();
        demo.displayCollection();
    }
}

/ ******************* main.cpp ************************* /

Enter the words separated by space: hello hi bye hello bye
Key: hi, Value: 1
Key: hello, Value: 2
Key: bye, Value: 2

/ *********************** pair.cpp ****************** / < / p>

    func readSleep(date: Date) {

        guard let sleep = HKSampleType.categoryType(forIdentifier: .sleepAnalysis) else {
            print("Sample type not available")
            return
        }

        let startDate = convertSleepStartDate(StartDate: date)
        let endDate = convertSleepEndDate(EndDate: date)
        let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
//        let asleepPredicate = HKQuery.predicateForCategorySamples(with: <#T##NSComparisonPredicate.Operator#>, value: <#T##Int#>)
//        let compound:NSCompoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicate, asleepPredicate])

        let sleepQuery = HKSampleQuery(sampleType: sleep,
                                                predicate: predicate,
                                                limit: HKObjectQueryNoLimit,
                                                sortDescriptors: nil) {
                                                    (query, samples, error) in

                                        guard
                                            error == nil,
                                        samples == samples as? [HKCategorySample] else {
                                                print("Something went wrong getting sleep: \(String(describing: error))")
                                                return
                                        }

                                        if let result = samples {

                                            result
                                                .compactMap({ $0 as? HKCategorySample})
                                                .forEach({ sample in
                                                    guard let sleepValue = HKCategoryValueSleepAnalysis(rawValue: sample.value) else {
                                                return
                                            }

                                            let isAsleep = sleepValue == .asleep
                                                    if isAsleep == true {
                                                        let total = samples?.map(self.calculateSleepHours(sample:)).reduce(0, {$0 + $1}) ?? 0
                                                        DispatchQueue.main.async {
                                                            self.userSleepHours = total / 60 / 2
                                                        }
                                                    } else {
                                                       return
                                                    }

                                            })
                                                        }
        }
        HKHealthStore().execute(sleepQuery)
    }

它给了我make函数一个错误;因为它不是在main范围内声明的,我也不明白为什么。 当涉及所有朋友功能和模板时,也会出现问题。该程序不想被编译。

2 个答案:

答案 0 :(得分:0)

make是全局函数。您的头文件应如下所示:

template<class T1,class T2>
class Pair
{
private :
  T1 first;
  T2 second;
public  :
  Pair(T1,T2);
  Pair(){};
  void operator=(const Pair& other);
  friend ostream& operator<<(ostream& out ,const Pair<T1,T2>& A);

};

template<class T1,class T2>
Pair<T1,T2>  make(T1  a , T2  b);

另外,您的代码还有其他问题。首先,所有代码都应位于头文件中,如上面评论中的链接问题所述。

另请参阅此问题以获取模板朋友问题

Overloading friend operator << for template class

答案 1 :(得分:0)

make是Pair类的函数 因此,将其与Pair类之类的对象一起使用:

#include<iostream>
#include<utility>
#include"Pair.h"
using namespace std;

int main()
{ Pair pObj;
  Pair<int,int> A= pObj.make(10,20);
  cout << A ;
  return 0;
}
相关问题