C++ 如何修复错误2298和2563
我正在为一项作业上课。它应该记录程序运行的时间和循环的次数,然后将这些信息放入文件中。现在,它给了我:C++ 如何修复错误2298和2563,c++,C++,我正在为一项作业上课。它应该记录程序运行的时间和循环的次数,然后将这些信息放入文件中。现在,它给了我: error 2298 "missing call to bound pointer to member function" 及 我很难找到解决这些问题的方法;它可能没有我现在做的那么复杂,但任何帮助都将不胜感激 #include <thread> #include <iostream> #include <fstream> #include <chr
error 2298 "missing call to bound pointer to member function"
及
我很难找到解决这些问题的方法;它可能没有我现在做的那么复杂,但任何帮助都将不胜感激
#include <thread>
#include <iostream>
#include <fstream>
#include <chrono>
#include <string>
using namespace std;
class Timer {
private:
typedef chrono::high_resolution_clock Clock;
Clock::time_point epoch;
public:
Timer() {
epoch = Clock::now();
}
Clock::duration getElapsedTime() { return Clock::now() - epoch; }
};
int loopCount()
{
for (int count=0;count<=100;) {
count++;
}
return count;
}
int fProjectDebugFile()
{
fstream debugFile;
debugFile.open ("FinalProjectDebugger.txt", fstream::out | fstream::app);
string jar(Timer);
cout << jar << endl << loopCount() << endl;
debugFile.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级计时器{
私人:
typedef chrono::高分辨率时钟;
时钟:时间点历元;
公众:
计时器(){
历元=时钟::现在();
}
时钟::持续时间getElapsedTime(){返回时钟::现在()-epoch;}
};
int loopCount()
{
对于(int count=0;count您不能访问循环外部的循环变量
因此,将声明移到循环之外,即替换此:
int loopCount(){
for(int count=0;count<=100;){
count++;}
return count;
}
没有什么意义。Timer
是一种类型,所以stringjar(Timer)
声明一个名为jar
的函数,该函数将计时器
对象作为参数,并返回一个字符串
这是您所有的代码吗?虽然您发布的代码触发编译器错误,但它不会触发您列出的两条错误消息中的任何一条?此代码在解决计数
和警告:函数指针jar
永远不会为空,但它没有指向成员函数的指针。@JaMiT,jar
是字符串
而不是函数指针。此外,您收到的错误消息类型取决于您使用的编译器/链接器。@SidS no,jar
被声明为使用计时器的函数
参数并返回一个string
(gcc:“std::string jar(Timer)”的地址永远不会为空;clang:address of function“jar”将始终计算为“true”。)哪个编译器给了您一个警告或错误,其中提到了指向成员函数的指针?@JaMiT,是的,我现在明白您的意思了。string jar(Timer);
声明一个函数,该函数将计时器
对象作为参数,并返回一个字符串
。编辑是正确的。您的第一次剪切是正确的,但有更多提示了特定错误。编辑得到了它。
int loopCount(){
for(int count=0;count<=100;){
count++;}
return count;
}
int loopCount()
{
int count = 0;
while (count <= 100)
{
count++;
}
return count;
}
class Timer
...
string jar(Timer);