我有这个奇怪的问题-我定义并声明了函数,没有错别字,没有外部库,没有命名空间失败,没有模板,没有其他线程提及-但我仍然为函数调用得到“未定义符号”。
我有以下代码:
在一个.cpp文件中:
string print(const FanBookPost& post) {
std::stringstream ss;
// notice! post.getOwner.getId! owner needs to be fan
ss << post.getOwner().getId() << ": " << post.getContent() << "(" << post.getNumLikes()
<< " likes)";
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
该文件包含FanBookPost.h。
然后我在FanBookPost.h中:
class FanBookPost {
private:
Fan owner;
std::string content;
int numOfLikes;
int seqNum;
public:
// constructors
int getNumLikes();
std::string getContent();
Fan getOwner();
int getNumLikes() const;
std::string getContent() const;
Fan getOwner() const;
};
Run Code Online (Sandbox Code Playgroud)
如您所见,我准备了const和常规版本。在第一个.cpp文件中,“ post”函数获取的是const。
我在FanBookPost.cpp中在这里实现了这些功能:
class FanBookPost {
private:
Fan owner;
std::string content; …Run Code Online (Sandbox Code Playgroud) 代码1:
struct demo
{
int a;
}d[2];
int main()
{
d[0].a=5;
d[1]=d[0];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常
代码2:
struct demo
{
int a;
}d[2];
int main()
{
d[0].a=5;
d[1]=d[0];
if(d[0]==d[1])
{
printf("hello");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码给出错误
error: invalid operands to binary == (have 'struct demo' and 'struct demo')
Run Code Online (Sandbox Code Playgroud)
为什么在Code 2中出现此错误?
我正在阅读我朋友的代码我看到了:
#include <stdio.h>
#include <conio.h>
void main()
{
char string1[125], string2 [10];
int i, j;
printf("\nstring 1: ");
gets(string1);
printf("\nNstring2 : ");
gets(string2);
i = 0;
while (string1[i] != 0)
{
j = 0;
while (string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0)
;//i dont know what it mean and why we can put ;after while loop
if (string1[i-1] != 0 && string2[j-1] == 0)
printf("\nfound at position %d", i-j);
}
getch();
}
Run Code Online (Sandbox Code Playgroud)
为什么我们可以放在;while循环后,任何人都可以帮忙?
我有以下代码:
long int compute_data_length(unsigned char* buf, int data_offset) {
long int len;
if(!buf)
return -1;
switch(data_offset) {
case 2: {
len = (buf[2] << 8) | buf[3];
}
break;
case 8: {
len = (buf[2] << 56) |
(buf[3] << 48) |
(buf[4] << 40) |
(buf[5] << 32) |
(buf[6] << 24) |
(buf[7] << 16) |
(buf[8] << 8) |
(buf[9] );
}
break;
default: len = -1; break;
}
return len;
}
Run Code Online (Sandbox Code Playgroud)
编译时,我收到以下警告:
math_fun.c:240:21:警告:左移计数> =类型的宽度[默认启用] len =(buf …
我有以下代码片段:
main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出
0 50 0
Run Code Online (Sandbox Code Playgroud)
我不知道我理解的第一个值怎么printf来0.当值k与之比较时35,理想情况下它应该返回(并因此打印)1,但它如何打印为零?产生的其他两个值 - 50并且0都是正确的,因为在第二个值中,k的值被视为50,而对于第三个值,k的值(即35)被比较40.因为35 < 40,所以它打印0.
任何帮助将不胜感激,谢谢.
**更新**
在研究了关于这个主题的更多内容之后undefined behavior,我在一本关于C的书中看到了这一点,最后给出了源代码.
调用约定 调用约定表示在遇到函数调用时参数被传递给函数的顺序.这里有两种可能性:
C语言遵循第二顺序.
考虑以下函数调用:
fun (a, b, c, d ) ;
Run Code Online (Sandbox Code Playgroud)
在这个调用中,参数是从左向右还是从右向左传递并不重要.但是,在某些函数调用中,传递参数的顺序成为一个重要的考虑因素.例如:
int a = 1 ;
printf …Run Code Online (Sandbox Code Playgroud) 我已经读过,它在某些C标准(可能是99?)中未定义,当修改const时会发生什么.但是一位学生向我提供了一些我修改过的代码.
我看不出常量变量的地址有什么特别之处a.我验证了它&a并且b是相同的,因此编译器不会巧妙地指向其他位置.然而,当我分配时*b,const值不会改变.
我没有运行优化.当我使用-g标志进行编译以进行调试并进入代码时,我得到了我期望的结果(变量的内存位置发生了a变化).然而,下面提供的代码并未反映更新后的价值a.
即使在调试模式下,这个temp现在也被放在寄存器中,没有优化吗?
#include <iostream>
using namespace std;
int main(){
const int a = 15;
cout << a << '\n';
int * b= (int*)&a;
cout << &a << "\n";
cout << b << "\n";
*b = 20;
cout << *b << '\n';
cout << a << '\n';
int x = a;
cout << x << '\n';
x = *b;
cout << …Run Code Online (Sandbox Code Playgroud) 对不起这个愚蠢的问题我很抱歉.我有C程序提示用户输入年龄和名称,然后将年龄和名称打印到屏幕上.这是我从书中读到的练习.
这个程序:
#include <stdio.h>
int main (void) {
int age;
char name[20];
puts("Enter your age:");
scanf("%d",&age);
fflush(stdin);
puts("Enter your name:");
scanf("%s",name);
printf("Your age is %d\n",age);
printf("Your name is %s\n",name);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我向第一个输入额外的字符时,scanf()程序终止并将额外的字符分配给下一个scanf()
然后我更改了代码,并添加了命名函数clear_buff()并使用其中的fgets函数clear_buff()来读取流上的剩余字符.代码按照我的预期工作.
#include <stdio.h>
#define MAXLEN 80
void clear_buff(void);
int main (void) {
int age;
char name[20];
puts("Enter your age:");
scanf("%d",&age);
clear_buff();
puts("Enter your name:");
scanf("%s",name);
printf("Your age is %d\n",age);
printf("Your name is %s\n",name);
return 0;
}
void clear_buff(void){ …Run Code Online (Sandbox Code Playgroud) 我找到了这段代码,并想知道我是否真的应该在我的真实项目中实现这样的东西.
令我困惑的事情是
这需要更多的编译时间,但我不应该以运行时为代价来打扰编译时间.
如果N真的是一个非常大的数字呢?源代码中是否有文件大小限制?
或者它的好事要知道,而不是实施?
#include <iostream>
using namespace std;
template<int N>
class Factorial {
public:
static const int value = N * Factorial<N-1>::value;
};
template <>
class Factorial<1> {
public:
static const int value = 1;
};
int main() {
Factorial<5L> f;
cout << "5! = " << f.value << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
5! = 120
Run Code Online (Sandbox Code Playgroud)
稍微修改一下,因为我正在玩代码,发现了
Factorial<12> f1; // works
Factorial<13> f2; // doesn't work
Run Code Online (Sandbox Code Playgroud)
错误:
undefined reference to `Factorial<13>::value'
Run Code Online (Sandbox Code Playgroud)
是不是可以12进一步深入?
我正在通过GDB对用C++编写的项目进行调试,并发现在GNU C++编译器中没有警告或错误地修改了const.
这不是我正在调试的程序,但这是我目睹的行为的一个例子:
#include <iostream>
int main(int argc, char *argv[]) {
const int x = 10;
int *px = (int *)&x;
++*px;
std::cout << "*px: " << *px << "\n";
std::cout << "x: " << x << "\n";
for (int i = 0; i < x; ++i)
std::cout << i+1 << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能代表其他编译器,因为我只使用GNU C++编译器4.9.2版进行了测试.为什么允许这样的事情?这打破了const对象的整个点.
我用g ++ main.c -Wall -Werror编译了上面的代码
输出:
*px: 11
x: 10
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud) 我试图创建通用的流类持有者,但似乎我无法传递std::cout给它,代码:
#include <iostream>
struct x
{
std::ostream &o;
x(std::ostream &o):o(o){}
};
int main()
{
x(std::cout);
x.o<<"Hi\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时失败:
c++ str.cc -o str -std=c++11
str.cc: In function ‘int main()’:
str.cc:11:14: error: invalid use of qualified-name ‘std::cout’
str.cc:12:4: error: expected unqualified-id before ‘.’ token
Run Code Online (Sandbox Code Playgroud)
为什么?