3 IntelliSense: объявление несовместимо с & lt; error-type & gt; Аэропорт :: getDetails (STD :: строка) & Quot;

получая эту ошибку

я знаю, что это как-то связано с несовпадением деклараций и определений, но, похоже, я не могу это понять.

Любая помощь будет оценена 🙂

IntelliSense: declaration is incompatible with "<error-type>
Airport::getDetails(std::string)" (declared at line 16 of "z:\documents\visual studio 2010\projects\oo_cw\oo_cw\Airport.h")
z:\documents\visual studio 2010\projects\oo_cw\oo_cw\airport.cpp    50  22  OO_CW

Вот мой заголовочный файл и файл cpp

заголовок

#pragma once
#include <string>
#include "std_lib_facilities.h"

class Airport
{
public:
Airport();
~Airport(void);
//Modifiers
void setName(string);
void addConnection(string,double,double);
void setTax(double);
//Accessors
string getName();
connections getDetails(string) const ;
double getTax();

private:
string name;
Vector<connections> destinations;
double tax;
};

CPP

#include "Airport.h"#include <string>
#include "std_lib_facilities.h"using namespace std;

struct connections {
string destName;
double price;
double time;
};

Airport::Airport()
{
name = "";
tax = 0.0;

};Airport::~Airport(void)
{
};

void Airport::setName(string airportName){
Airport::name = airportName;
}
void Airport::setTax(double airportTax){
tax = airportTax;
}

void Airport::addConnection(string conName, double conPrice, double conTime){
connections newConnection;
newConnection.destName = conName;
newConnection.price = conPrice;
newConnection.time = conTime;
destinations.push_back(newConnection);
}

string Airport::getName(){
return name;
}

double Airport::getTax(){
return tax;
}

connections Airport::getDetails(string reqName) const {
for(int i =0;i<destinations.size();i++){
if(destinations[i].destName==reqName){
return destinations[i];
}
}
}

1

Решение

Вы не декларируете connections прежде чем использовать его в «Airport.h».

1

Другие решения

  1. Вы должны положить connections определение выше класса Airport определение в заголовочном файле.
  2. Ваша функция Airport::getDetails() должен вернуть что-то для тривиального случая, где destinations.size() == 0(см. код ниже).
  3. Есть ли особая причина не использовать std::vector в месте Vector?

connections Airport::getDetails(string reqName) const {
for (int i = 0; i<destinations.size(); i++){
if (destinations[i].destName == reqName){
return destinations[i];
}
}
return connections{"", 0.0, 0.0};
}
1