函数定义 #包括 #包括 #包括“NamedStorm.h” 使用名称空间std; //确保displayOutput方法正常工作 std::ostream&operator,visual-c++,external,symbols,lnk2001,Visual C++,External,Symbols,Lnk2001" /> 函数定义 #包括 #包括 #包括“NamedStorm.h” 使用名称空间std; //确保displayOutput方法正常工作 std::ostream&operator,visual-c++,external,symbols,lnk2001,Visual C++,External,Symbols,Lnk2001" />

Visual c++ 错误LNK2001:未解析的外部符号?I';我完全迷路了 我是C++新手,我把我以前的java代码翻译成C++。我遇到了太多的错误,以至于我几乎放弃了希望。我还试图修复主文件中的一个错误,我试图调用主文件中的一个函数,但是我遇到了疯狂的语法错误,我不知道出了什么问题。几周来,我一直在谷歌上搜索如何在main.cpp中修复这些错误。如果可以的话,我很感激你的帮助 // NamedStorm.cpp // CPP=> Function definition #include <iostream> #include <string> #include "NamedStorm.h" using namespace std; // Makes sure that the displayOutput method work properly std::ostream& operator<<(ostream& out, const NamedStorm& namedStorm); // Default Constructor definition (removed parameter due to issues) NamedStorm::NamedStorm(){ } // Overload construtor definition NamedStorm::NamedStorm(string sName, double wSpeed, string sCat,double sPress){ stormName = sName; stormCategory = sCat; stormPressure = sPress; stormCount++; } // Destructor definition NamedStorm::~NamedStorm(){} // Accessor function definition void NamedStorm::displayOutput(NamedStorm storm[]){ for(int i = 0; i < sizeof(storm); i++){ cout << storm[i] << "\n"; } } void NamedStorm::sortByNames(NamedStorm storm[]){ cout << "Sorting array in decsending alphabetical order by names..." << endl; for(int k = 1; k < 4; k++){ for(int i = 0; i < 4 - k; i++){ if((storm[i].getName()).compare(storm[i+1].getName()) > 0){ NamedStorm temp; temp = storm[i]; storm[i] = storm[i+1]; storm[i+1] = temp; } } } } void NamedStorm::getAverageWindSpeed(NamedStorm storm[]){ double averageWSpeed = 0.0; double totalWindSpeed = 0.0; totalWindSpeed = storm[0].maxWindSpeed + storm[1].maxWindSpeed + storm[2].maxWindSpeed + storm[3].maxWindSpeed + storm[4].maxWindSpeed; averageWSpeed = totalWindSpeed / sizeof(storm); cout << "The average max wind speeds of storms: " << averageWSpeed << "mph"<< endl; } void NamedStorm::getAverageStormPressure(NamedStorm storm[]){ double averageSPress = 0.0; double totalStormPressure = 0.0; totalStormPressure = storm[0].getStormPressure() + storm[1].getStormPressure() + storm[2].getStormPressure() + storm[3].getStormPressure() + storm[4].getStormPressure(); averageSPress = totalStormPressure / 5; cout << "The Average storm pressure: " << averageSPress << " mb" << endl; } int NamedStorm::getStormCount(){ return stormCount; } double NamedStorm::getStormPressure(){ return stormPressure; } double NamedStorm::getWindSpeed(){ return maxWindSpeed; } string NamedStorm::getStormCategory(){ return stormCategory; } string NamedStorm::getName(){ return stormName; } // Mutator function definition //NamedStorm.cpp //CPP=>函数定义 #包括 #包括 #包括“NamedStorm.h” 使用名称空间std; //确保displayOutput方法正常工作 std::ostream&operator

Visual c++ 错误LNK2001:未解析的外部符号?I';我完全迷路了 我是C++新手,我把我以前的java代码翻译成C++。我遇到了太多的错误,以至于我几乎放弃了希望。我还试图修复主文件中的一个错误,我试图调用主文件中的一个函数,但是我遇到了疯狂的语法错误,我不知道出了什么问题。几周来,我一直在谷歌上搜索如何在main.cpp中修复这些错误。如果可以的话,我很感激你的帮助 // NamedStorm.cpp // CPP=> Function definition #include <iostream> #include <string> #include "NamedStorm.h" using namespace std; // Makes sure that the displayOutput method work properly std::ostream& operator<<(ostream& out, const NamedStorm& namedStorm); // Default Constructor definition (removed parameter due to issues) NamedStorm::NamedStorm(){ } // Overload construtor definition NamedStorm::NamedStorm(string sName, double wSpeed, string sCat,double sPress){ stormName = sName; stormCategory = sCat; stormPressure = sPress; stormCount++; } // Destructor definition NamedStorm::~NamedStorm(){} // Accessor function definition void NamedStorm::displayOutput(NamedStorm storm[]){ for(int i = 0; i < sizeof(storm); i++){ cout << storm[i] << "\n"; } } void NamedStorm::sortByNames(NamedStorm storm[]){ cout << "Sorting array in decsending alphabetical order by names..." << endl; for(int k = 1; k < 4; k++){ for(int i = 0; i < 4 - k; i++){ if((storm[i].getName()).compare(storm[i+1].getName()) > 0){ NamedStorm temp; temp = storm[i]; storm[i] = storm[i+1]; storm[i+1] = temp; } } } } void NamedStorm::getAverageWindSpeed(NamedStorm storm[]){ double averageWSpeed = 0.0; double totalWindSpeed = 0.0; totalWindSpeed = storm[0].maxWindSpeed + storm[1].maxWindSpeed + storm[2].maxWindSpeed + storm[3].maxWindSpeed + storm[4].maxWindSpeed; averageWSpeed = totalWindSpeed / sizeof(storm); cout << "The average max wind speeds of storms: " << averageWSpeed << "mph"<< endl; } void NamedStorm::getAverageStormPressure(NamedStorm storm[]){ double averageSPress = 0.0; double totalStormPressure = 0.0; totalStormPressure = storm[0].getStormPressure() + storm[1].getStormPressure() + storm[2].getStormPressure() + storm[3].getStormPressure() + storm[4].getStormPressure(); averageSPress = totalStormPressure / 5; cout << "The Average storm pressure: " << averageSPress << " mb" << endl; } int NamedStorm::getStormCount(){ return stormCount; } double NamedStorm::getStormPressure(){ return stormPressure; } double NamedStorm::getWindSpeed(){ return maxWindSpeed; } string NamedStorm::getStormCategory(){ return stormCategory; } string NamedStorm::getName(){ return stormName; } // Mutator function definition //NamedStorm.cpp //CPP=>函数定义 #包括 #包括 #包括“NamedStorm.h” 使用名称空间std; //确保displayOutput方法正常工作 std::ostream&operator,visual-c++,external,symbols,lnk2001,Visual C++,External,Symbols,Lnk2001,我可能会建议将它分解成更小的块(注释掉所有内容),然后一次添加一个项目,确保在执行时理解每个项目 在“identifier not found”(标识符未找到)上,看起来您试图调用静态类方法,但它没有以类的名称作为前缀。我认为不是: displayOutput(); 在main()中,您需要: NamedStorm::displayOutput(); 但即使这样也不完全正确,因为该方法采用storm数组,并且没有提供任何参数。因此,我认为正确的签名应该是: NamedStorm stormA

我可能会建议将它分解成更小的块(注释掉所有内容),然后一次添加一个项目,确保在执行时理解每个项目

在“identifier not found”(标识符未找到)上,看起来您试图调用静态类方法,但它没有以类的名称作为前缀。我认为不是:

displayOutput();
在main()中,您需要:

NamedStorm::displayOutput();
但即使这样也不完全正确,因为该方法采用storm数组,并且没有提供任何参数。因此,我认为正确的签名应该是:

NamedStorm stormArray[2];
NamedStorm::displayOutput( stormArray );
如果打算调用一个全局“displayOutput()”,那么我看不到有一个定义可以解释这种情况下的错误消息

第二部分-ostream操作员==============================================================

啊。。。你真的是从顶端开始的!好的,我也不能让它工作-我看了所有这些关于如何正确定义模板的链接,等等。。。但问题和错误消息一样简单。引用未解析,因为它不存在!您定义了一个新运算符,但未提供实现。您只需提供实现,如下所示:

// this declares the operator, but it's still undefined
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm);

// add this to *define* it - otherwise it has no body so the linker looks in all the
// obj files, can't find it and gives the unresolved reference error
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm)
{
//print("we needed an implementation!");
return out;
}
相反,请更明确地使用您想要的名称空间,例如:

std::string myString = "we know which 'string' this is";
简单地说“使用一切…”似乎更容易,直到你花了半天的时间跟踪一个bug,却发现你甚至没有看到正确的类


呼..我现在休息;-)

好的,LNK2001错误不再出现,现在它只是main.cpperor LNK2001:未解析的外部符号“private:static int NamedStorm::stormCount”(?)中调用函数的“identifier not found”错误?stormCount@NamedStorm@@0公顷)。也许是私有变量?并不是说“stormCount”是私有的,这没关系,因为你是从类内部引用它的。这里的问题是它是静态的,但是您试图从构造函数引用它,构造函数是一个实例方法。如果在构造函数中说“NamedStorm::stormCount++”,它应该可以正常工作。(也可以在析构函数中减少它-dunno取决于您的需要)。很高兴它为你工作。我决定删除静态(仍然需要它,也许我不需要它)现在所有IM留下的是这个非常奇怪的错误LNK2019:未解决的外部符号“类STD::Basic流…dDADADADADADADADDY我对你说的话失去了,决定退出直到我得到基本知识。你推荐任何免费的C++吗?(或付费)新手?
// this declares the operator, but it's still undefined
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm);

// add this to *define* it - otherwise it has no body so the linker looks in all the
// obj files, can't find it and gives the unresolved reference error
std::ostream& operator<<(std::ostream& out, const NamedStorm& namedStorm)
{
//print("we needed an implementation!");
return out;
}
using namespace std;
std::string myString = "we know which 'string' this is";