我很难将其编译.我认为它与静态变量有关,但我不是100%肯定我在做什么.以下是我不断收到的错误消息:
体系结构x86_64的未定义符号:"Counter :: nCounters",引自:main.o中的Counter :: Counter(int,int)
main.o中的Counter :: getNCounters()
Counter.o中的Counter :: Counter(int,int)
Counter.o中的Counter :: getNCounters()
ld:找不到架构x86_64的符号
clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
这是头文件:
#ifndef project1_Counter_h
#define project1_Counter_h
class Counter
{
private:
int counter;
int limit;
static int nCounters;
public:
Counter(int, int);
void increment();
void decrement();
int getValue();
static int getNCounters();
};
#endif
Run Code Online (Sandbox Code Playgroud)
这是.cpp文件:
#include "Counter.h"
Counter::Counter(int a, int b)
{
counter = a;
limit = b;
nCounters++;
}
void Counter::increment()
{
if (counter < limit)
counter++;
}
void Counter::decrement()
{
if (counter > 0)
counter--; …Run Code Online (Sandbox Code Playgroud) 我成功完全重载了一元++,--postfix /前缀运算符,我的代码工作正常,但在使用(++obj)++语句时,它返回意外的结果
这是代码
class ABC
{
public:
ABC(int k)
{
i = k;
}
ABC operator++(int )
{
return ABC(i++);
}
ABC operator++()
{
return ABC(++i);
}
int getInt()
{
return i;
}
private:
int i;
};
int main()
{
ABC obj(5);
cout<< obj.getInt() <<endl; //this will print 5
obj++;
cout<< obj.getInt() <<endl; //this will print 6 - success
++obj;
cout<< obj.getInt() <<endl; //this will print 7 - success
cout<< (++obj)++.getInt() <<endl; //this will print 8 …Run Code Online (Sandbox Code Playgroud) int main() {
float a = 20000000;
float b = 1;
float c = a+b;
if (c==a) { printf("equal"); }
else { printf("not equal");}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我跑这个时,它说"平等".但当我将a的值更改为2000000(少一个零)时答案是否定的.为什么?
当我在K&R上练习时,我发现了一个非常有趣的问题:
代码如下:
include <stdio.h>
main()
{
int c;
int bn;
bn=0;
while((c=getchar())!=EOF)
{
if(c==' ')
bn++;
}
printf("blanks counter:%d\n",bn);
}
Run Code Online (Sandbox Code Playgroud)
代码函数是统计空白的数量
我一次输入所有单词,没有使用退格键,就完成了.
$ ./a.out
I have a dream
blanks counter:3
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用退格键,输入过程中会发生什么?
我推测当按下空格键时,getchar()函数应该得到这个事件,然后执行bn ++,所以即使我后来按退格键删除这个空白空间,bn变量的值也不会改变.但实践的结果与我的推测不同,实践的程序如下:
第一步:输入第一个空格

第二步:使用退格键删除空白空间
第三步:完成剩余的字符输入

为什么结果是3而不是4?为什么bn变量的值会被按下退格键改变?
请给我一些想法,谢谢!
char input1;
std::cout << "input1 : ";
std::cin >> input1;
int input2;
std::cout << "input2 : ";
std::cin >> input2;
std::cout << input1 << std::endl;
std::cout << input2 << std::endl;
return 0;
Run Code Online (Sandbox Code Playgroud)
我在input1写了'a',在input2写了'a'.
Ouput is like this.
input1 : a
input2 : a
a
-858993460
Run Code Online (Sandbox Code Playgroud)
我好奇......'一个'章程在十九月份是97.为什么打印-858993460?'a'不会自动转换为97?为什么?
我是否需要使用typedef才能构建递归结构?我尝试使用以下代码但没有成功:
struct teste
{
int data;
int data2;
struct teste to_teste;
};
Run Code Online (Sandbox Code Playgroud) 在C++中,单例中的所有成员都可以是静态的,还是尽可能的?我的想法是,无论如何全球只有一个例子.
在搜索时我确实在C#中找到了很多关于静态类的讨论,但对此并不熟悉.想了解它.
无论你有什么想法,请评论.
我有一个Fraction类如下.
class Fraction
{
int num, den ;
public:
//member functions here
} ;
Run Code Online (Sandbox Code Playgroud)
我读了一些书,我认为'有效的c ++'最好将加法运算符作为非成员函数重载.给出的原因是它允许交换加法.这是我为加法运算符重载函数的原型.
Fraction operator + (const Fraction &obj, const int add_int) ;
Run Code Online (Sandbox Code Playgroud)
在这里,当我打电话时,我必须这样做.
f1 + 2 ;
Run Code Online (Sandbox Code Playgroud)
但它不会这样.
2 + f1 ;
Run Code Online (Sandbox Code Playgroud)
为此,我将不得不再次编写该函数并更改其中的参数顺序.
我想知道是否有一种方法可以让我一次重载函数并执行可交换的加法?
对不起伙计我知道我的英语不好,但我做了一些例子,以便我的问题更加清晰.
a.cpp
#include <iostream>
using namespace std;
void funcfoo(){
cout << "test only" << endl;
}
int varfoo = 10;
Run Code Online (Sandbox Code Playgroud)
b.cpp
#include <iostream>
using namespace std;
extern void funcfoo();
extern int varfoo;
int main(){
funcfoo();
cout << varfoo;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我像这样编译它"cl b.cpp a.cpp"
我的问题是.为什么我在void funcfoo()之前删除"extern关键字"它工作正常,但是当我在int var foo之前删除extern关键字时出现错误?
有什么问题:我只想指向int并将该int值赋予0.
int* p;int* q;
*p = 0; *q = 0;
cout<<"p = "<<*p<<" q = "<<*q<<endl;
Run Code Online (Sandbox Code Playgroud)
这很烦人
作品:
int* p;
*p = 0;
cout<<*p<<endl;
Run Code Online (Sandbox Code Playgroud)
崩溃:
int* p;
int* q;
*p = 0;
*q = 0;
cout<<*p<<endl;
Run Code Online (Sandbox Code Playgroud)