我正在尝试使用MinGW 4.7.2编译一个简单的C++程序,但是由于大量的错误和警告而感到沮丧.
/*\ program.h \*/
typedef struct
{ int member0;
int member1;
int member2;
int member3;
} structure;
void function(&structure);
Run Code Online (Sandbox Code Playgroud)
/*\ program.cpp \*/
#include "program.h"
int main(void)
{ structure *instance;
function(instance);
}
void function(&structure)
{ // nothing
}
Run Code Online (Sandbox Code Playgroud)
function取一个地址structure.指针instance包含structure随后传递给的地址function.
很简单,但编译器并不开心.
g++ program.cpp
In file included from program.cpp:1:0:
program.h:8:14: error: variable or field 'function' declared void
program.h:8:25: error: expected primary-expression before ')' token
program.cpp: In function 'int main()':
program.cpp:5:19: error: 'function' was …Run Code Online (Sandbox Code Playgroud) 有谁知道为什么下面的代码不适用于chars?它适用于ints但是当我想用char它来初始化结构时它会失败并发出如下警告:
warning: assignment makes integer from pointer without a cast
Run Code Online (Sandbox Code Playgroud)
我不知道这个警告意味着什么.
#include <stdio.h>
#include <stdlib.h>
struct complex {
int re;
int im;
char name;
};
struct complex initialize (int k, int l, char nazwa)
{
struct complex x;
x.re = k;
x.im = l;
x.name= nazwa;
return x;
}
int main ()
{
struct complex e;
struct complex f;
int a;
int b;
char o;
int c;
int d;
char p;
a=5;
b=6;
o="t"; …Run Code Online (Sandbox Code Playgroud) 我有7个类继承了"Base"类:
对于这7个类中的3个,我必须添加一个非静态方法(完全相同的代码),但是这个方法不应该对其他类可见,我不知道如何组织我的代码.
有人可以帮帮我吗?
编辑: 我不能这样做:
因为在非静态方法中,我使用了在Class1,Class2和Class3类中声明的属性(我无法移动它,它由Entity Framework处理).示例:
this.var
Run Code Online (Sandbox Code Playgroud) 我已经开始编写一个程序,可以在一个结构中读取和存储团队信息和存储,重新排序并打印结果.
首先,我正在尝试阅读团队名称并将其存储在结构中的成员中,然后阅读团队分数并将其存储在另一个成员中.
但是,只要我调试文件,它就会崩溃.它甚至没有正确启动.我收到这个错误
PA2.exe中0xFEFEFEFE处的未处理异常:0xC0000005访问冲突执行位置0xFEFEFEFE.
我已经回溯到足以弄清楚它是在while循环中的某个地方,但无法弄清楚它有什么问题.
我正在使用visual studio 2012,当我构建它时我没有错误.
#include "stdafx.h"
#include "string.h"
#include <stdio.h>
struct team
{
char name[20];
int no_games;
int points;
int goals_scored;
int goals_let;
};
int main(void)
{
FILE *input2a;
FILE *input2b;
int error1;
int error2;
int i;
char team1_name[20];
char team2_name[20];
int team1_goals;
int team2_goals;
struct team teamlist[20];
error1 = fopen_s(&input2a, "C:\\Users\\New PC\\Desktop\\input2a.dat", "r");
i = 0;
while (i < 20)
{
i ++;
fscanf_s(input2a, "%s", teamlist[i].name);
}
error2 = fopen_s(&input2b, "C:\\Users\\New PC\\Desktop\\input2b.dat", "r");
while (fscanf_s(input2b, …Run Code Online (Sandbox Code Playgroud) a是指向结构的指针,b是结构
a++ -> b
Run Code Online (Sandbox Code Playgroud)
上述代码的评估顺序为
((a++) -> b)
Run Code Online (Sandbox Code Playgroud)
要么
(a -> b) ++
Run Code Online (Sandbox Code Playgroud) 在我不小心使用的代码中
list* Head = malloc(sizeof(list*));
Run Code Online (Sandbox Code Playgroud)
而不是正确的
list* Head = malloc(sizeof(list));
Run Code Online (Sandbox Code Playgroud)
创建一个新的list类型节点,但以后工作得很好.
所以我的问题是为什么它能正常工作?
我char在结构中只声明了类型成员.
#include <stdio.h>
struct st
{
char c1;
char c2;
char c3;
char c4;
char c5;
};
int main() {
struct st s;
printf("%zu\n", sizeof(s));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: [ 现场演示 ]
5
Run Code Online (Sandbox Code Playgroud)
那么,为什么结构中只有char类型成员没有填充?
我有一个字符串变量,其中包含结构的名称.此结构在头文件中声明.我想基于结构名称的值创建一个结构的对象,该结构名称保存在C++的字符串变量中.
struct myStruct{
int a;
char b;
};
string structName = "myStruct";
// Instantiate a structure variable [like this: "struct myStruct"]
Run Code Online (Sandbox Code Playgroud)
有人可以帮我这个吗?
请让我知道如何将指针作为函数参数传递给C中的结构数组.
以下是我的代码.
#include <stdio.h>
#include<strings.h>
typedef struct _Alert
{
char MerchantNo[21];
time_t last_update;
} Alert;
typedef Alert *PALERT;
int set(PALERT palertMerch[5], int *merchnoIndex, char * txnMerchant)
{
strcpy(palertMerch[*merchnoIndex]->MerchantNo, txnMerchant);
*(merchnoIndex) = *(merchnoIndex) + 1 ;
return 0;
}
int main()
{
Alert alert[5];
for(int i =0; i<5;i++)
{
memset(alert[i].MerchantNo, 0x00, 21);
alert[i].last_update = (time_t)0;
}
char *p = "SACHIN";
int index = 0;
set(alert[5], index, p);
}
Run Code Online (Sandbox Code Playgroud)
错误信息
"3.c", line 34: argument #1 is incompatible with prototype:
prototype: pointer to …Run Code Online (Sandbox Code Playgroud) 我已经声明了一个结构"node",它有一个成员变量'm',然后定义了两个变量,如下所示
struct node t, *p;
Run Code Online (Sandbox Code Playgroud)
在稍后的节目给我分配的地址t到p:
p = &t;
Run Code Online (Sandbox Code Playgroud)
要访问我需要使用的成员变量p->m.
但我想使用*运算符,但写它*p.m会给出错误.为什么会这样?