• ベストアンサー

構造体のリンク?

こんにちは。 超初心者です。 どうぞよろしくお願いしますm(__)m 下記の三つのファイル、コンパイルすると「パブリックシンボル_high、_lowがi.obj、c.objの両方に定義されている」旨のエラーが出てしまいます。 ヘッダファイルに#ifndef~を書くくらいしか見当がつけられない初心者です(T_T) どこをどう直せばいいのか教えてください。 やりたいことは、high->i+high->j、low->i+low->jを関数一つに纏めて、mainから別ファイルに独立させることです。 *********c.h********* #ifndef __c__ #define __c__ struct type{ int i; int j; }*high,*low; extern int f(struct type *temp); #endif *********c.cpp********* #include"c.h" int f(struct type *temp); int f(struct type *temp){ return temp->i+temp->j; } *********i.cpp********* #include<iostream.h> #include"c.h" void main(void){ high->i=10; high->j=30; low->i=100; low->j=300; cout<<f(high)<<","<<f(low)<<endl; }

質問者が選んだベストアンサー

  • ベストアンサー
  • GOGOV
  • ベストアンサー率54% (12/22)
回答No.5

>この程度のものであれば、実態渡しもポインタ渡しも、速さ的には変わりませんか? うーん、どうなのかな、気にしたことないしな。おそらくそんなにかわらないでしょう。 それよりも、関数へのパラメータについての扱いは入力のみ、入出力、出力のみで 渡すパラメータを実体なのかポインタなのかを統一したほうがよいと思います。 見た目でin/outがわかりますしね。 もちろん入出力、出力のみはポインタ渡し。 入力のみについては実体渡しが一般的でないかと思います。ま、好みによりますが。 今回の場合は、入力のみだったので実体渡しを提案しました。

その他の回答 (4)

  • GOGOV
  • ベストアンサー率54% (12/22)
回答No.4

int f(struct type *temp) struct type high,low; high.i=10; high.j=30; low.i=100; low.j=300; cout<<f(&high)<<","<<f(&low)<<endl; ↑こんな感じですかね。 f()内で渡されたパラメータを変更しないのであれば、 ポインタ渡しでなく実体渡しでも良いですよ。 int f(struct type temp) f(high);

anpa_1978
質問者

お礼

なるほどー! ありがとうございます。 やっぱりポインタは初心者には理解しにくいですね(^^;; この程度のものであれば、実態渡しもポインタ渡しも、速さ的には変わりませんか?

  • GOGOV
  • ベストアンサー率54% (12/22)
回答No.3

high,lowを実体化すればよい。 *をなくすのと、 ->を使わないのと、 &をつけてfに渡す。

anpa_1978
質問者

補足

ありがとうございます。 high,lowの*をなくして、&で渡すということは、high,lowのアドレスをfに渡すことになるんですよね? 宣言は  int f(struct type &temp) ですか? その先をどのようにするのかわかりません(T_T)

回答No.2

> どこが間違ってるのでしょうか(T_T) struct type *high,*low; high->i=10; high->j=30; low->i=100; low->j=300; ここ。 初期化されていない(どこを指しているのかわからない)ポインタを使っています。

anpa_1978
質問者

お礼

できましたー!!! ポインタって難しいですね・・・。 ありがとうございます(^^) 私も早くアドバイスできるくらいになりたいです! 頑張りまス!!

回答No.1

ヘッダに high, low を書いてはいけません。多重定義となってしまいます。

anpa_1978
質問者

お礼

ありがとうございます! なるほど、そういうことだったんですね。 もう一つ、教えていただけますか? 先ほどのものはクリアできたので、今度はf()の関数内で、出力をしたいと思い、下記のように書きなおしました。 今度はコンパイルできるものの、実行時にエラーになってしまいます。 どこが間違ってるのでしょうか(T_T) コンパイラはBorland C++ 5.5.1です。 **********c.h********** #ifndef __c__ #define __c__ #include<iostream.h> struct type{ int i; int j; };                //直しました extern void f(struct type *temp); #endif **********c.cpp********** #include"c.h" void f(struct type *temp); void f(struct type *temp){ cout<<temp->i+temp->j<<endl;  //変更 } **********i.cpp********** #include"c.h" void main(void){ struct type *high,*low; high->i=10; high->j=30; low->i=100; low->j=300; f(high);             //書き足し f(low);             //書き足し // cout<<f(high)<<","<<f(low)<<endl; }

関連するQ&A

  • 複数のライブラリをリンクするときに構造体が衝突する

    C++,開発環境はVisual Studio Professional 2013です, ライブラリ1にxy座標の構造体 struct point { int x; int y; }; を定義しています. ライブラリ2にも全く同じ構造体を定義しています. ここで,あるプロジェクトからこれら2つのライブラリをリンクしようとしたら, error C2011: 'point' : 'struct' 型の再定義 というコンパイルエラーが出ます. ライブラリ1とライブラリ2にpoint構造体が記述された共通用のヘッダを参照させるという方法をとれば解決できるのでしょうが,この方法以外でこのエラーをなんとか回避する方法はないでしょうか. 構造体宣言は只の宣言であって実体をもたないため,中身が同じであればいくら記述が重複しても問題はないと聞いたことがあったので大丈夫だと思っていました. また,ライブラリ1とライブラリ2のpoint構造体の名前それぞれpoint1,point2にするなど違うものにするという方法でも回避できるのでしょうが,下記のメインコード内でコメントアウトされている部分のように,構造体の中身が全く同じなので互換性を持たせられるようにしたいのです. ちなみに,ライブラリ1,2はコンソールアプリケーション・空のプロジェクトで作成したあと,ソリューションのプロパティで「構成の種類」を「スタティックライブラリ(.lib)」にするという手順で作成しています. 以下,簡略コード ====ライブラリ1==== ----lib1.h---- #ifndef _H1_ #define _H1_ struct point { int x; int y; }; void print_lib1(struct point p); #endif ----lib1.cpp---- #include "lib1.h" #include <iostream> void print_lib1(struct point p) { std::cout << "lib1 : " << p.x << ", " << p.y << std::endl; } =====ライブラリ2====== ----lib2.h---- #ifndef _H2_ #define _H2_ struct point { int x; int y; }; void print_lib2(struct point p); #endif ----lib2.cpp---- #include "lib2.h" #include <iostream> void print_lib2(struct point p) { std::cout << "lib2 : " << p.x << ", " << p.y << std::endl; } ===メインコード=== ----main.cpp---- #include "../../lib1/lib1/lib1.h" #include "../../lib2/lib2/lib2.h" int main(void) { struct point val; val.x = 2; val.y = 3; print_lib1(val); //print_lib2(val); }

  • 基準値をランダムに選んでの選択ソート

    配列要素から基準値(pivot)をランダムに選び、K番目に小さい要素を検索するプログラムを書いているのですが、うまくいきません。かなり考えているのですが、何が間違っているのか全然わかりません。どなたか教えていただけないでしょうか? #include<stdio.h> #include<stdlib.h> #include<time.h> int pivotpoint; void partition3(int *s,int low,int high,int *pivotpoint) { int i,j,randspot; int tmp; int pivotitem; randspot=rand()%(high+1); pivotitem=s[randspot]; j=low; for(i=low+1;i<=high;i++){ if(s[i]<pivotitem){ j++; tmp=s[i]; s[i]=s[j]; s[j]=tmp; } } *pivotpoint=j; tmp=s[low]; s[low]=s[*pivotpoint]; s[*pivotpoint]=tmp; } int selection3(int *s,int low,int high,int k) { if(low==high) return s[low]; else{ partition3(s,low,high,&pivotpoint); if(k==pivotpoint) return s[pivotpoint]; else if(k<pivotpoint) return selection3(s,low,pivotpoint-1,k); else return selection3(s,pivotpoint+1,high,k); } } main() { int num,i,k; int high; int low=0; int s[1000]; struct timeval t1,t2; int seed=2; printf("How many elements?:"); scanf("%d",&high); printf("?n"); printf("What is the kth smallest number?:"); scanf("%d",&k); printf("?n"); srand(seed); for(i=0;i<high;i++){ s[i]=rand(); printf("%d ",s[i]); } printf("?n"); gettimeofday(&t1,0); num=selection3(s,low,high-1,k-1); gettimeofday(&t2,0); printf("Time=%dmicrosec?n", t2.tv_usec-t1.tv_usec); printf("The %dth smallest is %d?n ", k,num); }

  • C++でCの構造体をnewするとまずいのでしょうか?

    以下のようなCで書かれたヘッダがあったとして ---testc.h--- typedef struct __Test { int i; char c[30]; char* x; } Test; これを以下のようにnewして使うのは問題ないでしょうか? ---test.cpp--- extern "C" { #include test.h } int main(void) { Test* test = new Test(); test->i = 30; test->c[3] = 'a'; test->x = "aiueo"; return 0; }

  • 構造体の構造体 引数

    構造体の中の構造体の関数の引き渡し方法がわかりません。 下記ソースで試したのですが、うまくいきませんでした。 助言お願いいたします。 //repo.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define NUM 20 #define MAX 15 struct seiseki{ float shu[3]; }; struct seito{ char name[NUM]; int age; struct seiseki kekka; }; void input(struct seito *p); void s_input(struct seiseki *p); void ss_input(struct seiseki *data); int main(){ int i; struct seito data[2]; for(i=0;i<2;i++){ printf("------------------------------\n"); printf("%d人目",i+1); input(&data[i]); } printf("%f\n",data[0].kekka.shu[0]); printf("%f\n",data[0].kekka.shu[1]); printf("%f\n",data[0].kekka.shu[2]); //data[1]に格納できない。 printf("%f\n",data[1].kekka.shu[0]); printf("%f\n",data[1].kekka.shu[1]); printf("%f\n",data[1].kekka.shu[2]); return 0; } void input(struct seito *p){ printf("名前->"); scanf("%s",p->name); printf("年齢->"); scanf("%d",p->age); s_input(&(p->kekka)); } void s_input(struct seiseki *data){ printf("国語->"); ss_input(data); printf("算数->"); ss_input(data); printf("英語->"); ss_input(data); } //下記関数で成績をchar型で受け取り、数値化したい。 void ss_input(struct seiseki *data){ char p[100]; int i=0; static int o=0; scanf("%s",p); while( p[i] != '\0'){ if(isdigit(p[i])==0){ printf("再入力してください"); scanf("%s",p); } i++; } data->shu[o]=atof(p); printf("%f\n",data->shu[o]); o++; }

  • 構造体

    main.c を分割コンパイルすると次のようにエラーが出ます。 これもどこがいけないのかわかりません^^; 1/2 + 1/3 + ... 1/10 を計算するというものですが,たぶんadd の記述はまちがってるかもしれません。ただそれはきにしないでください。 よろしくおねがいします。 storage size of `a' isn't known storage size of `b' isn't known *****main.c****** #include "fraction.h" int main(){ int i; struct function a, b; a.numerator =1; a.denominator =2; for(i=3;i<11;i++){ b.numerator =1; b.denominator =i; add (&a,&b); } return (0); } *****fraction.h***** struct fraction{ int numerator;//bunsi int denominator;//bunbo }; extern struct fraction *kiyaku(); extern struct fraction *add(); extern void output(); ****sub.c***** struct fraction *add(struct fraction *a,struct fraction *b){ .... .... .... }

  • 構造体を使ったプログラム

    学校でC言語を勉強しています。(まだ初心者です) テストの成績を入力して、その結果を降順にソートしたいんですけど、 下記のプログラムでは、正常に動かないです。 struct seiseki { char nama; int sansuu; int rika; int goukei; }; ~~~~~~~~~~~~~~~~~~~~ struct seiseki class_a; struct seiseki class_b; struct seiseki *ptr1; struct seiseki *ptr2; ptr1 = class_a; ptr2 = class_b; ~~~~成績はあらかじめ入力済み~~~~ sout(class_a, 3); sout(class_b, 3); void sout(struct seiseki *p, int num) { struct seiseki temp; int count; int j; for (count = 1; num > count; count++) { temp = p[count]; for (j = count; j > 0 && p[j - 1].goukei < temp.goukei; j--) { p[j] = p[j - 1]; } p[j] = temp; p++; } } class_aだけを実行するプログラムだとちゃんと表示されますが、 一度でclass_aとclass_bを実行するプログラムだと表示がおかしくなります。 どなたか教えてください。

  • 構造体の使い方

    構造体の基本的な使い方を練習しようと思ったのですが、 なかなかスムーズにいきません。 p.d=10; の部分がおかしいようなのですが、色々試してもコンパイルできませんでした。 正しい使い方を教えていただけないでしょうか ソース ↓ #include<stdio.h> struct type{ int a; float b;  double c; }var,*p; main(){ c=9.87;      p=&var; p.d=10; p->b=1.2; printf("int:%d\n",*p); printf("float:%.1f\n",&p); printf("double:%.3lf\n",c); return 0; }

  • 構造体について

    私は今プログラミング(C++)を勉強しているのですが、構造体に苦戦しています。分からないことがいくつかあります。もし知っていたらどれでもいいですので、よろしければ教えてください。 1、たとえば構造体を宣言するときに struct abc{ int s; struct abc xx; struct abc yy; }; と宣言するときがあります。でも上記の例と下記の例との違いが分かりません。 struct abc{ int s; struct abc xx; struct abc yy; }abc; 2、たとえばメインの最初にこのように宣言されているとします。 int j, i; struct abc *kk, *nn, *mm; これはここに宣言したものがローカルで、1のときに宣言したものがグローバルと考えていいのでしょうか? 3、メインの中に次のようなプログラムがあったとします。 kk->xx = j; kk->yy = j; nn->yy = NULL; i = kk->xx; i = kk; これらがそれぞれどのような意味があるのでしょうか。 これらでわかることがあればぜひ教えてください。よろしくお願いいたします。

  • 中央値(median)を使用しての選択法(selection sort)

    中央値(median)を使用して、選択法により、K番目に小さい要素を見つけるプログラムを作成しようとしていますが、なかなかうまくいかず困っています。どなたか助けていただきたく。以下は作成しましたプログラムです。質問内容欄に入りきらない所は補足に掲載しますのでよろしく御願いいたします。 #include<stdio.h> #include<stdlib.h> #include<time.h> //int pivotpoint; int minimum(int i, int j) { if(i<=j) return i; else return j; } void partition2(int *s, int low, int high, int *pivotpoint) { const int arraysize = high-low+1; const int r=(arraysize/5)+1; int i,j,mark,first,last; int pivotitem; int t[5]; int x,y,tmp; //t=(int *)malloc(5); for(i=1;i<=r;i++){ first=low+5*i-5; last=minimum(low+5*i-1,arraysize); for(x=first;x<last;x++){ for(y=x+1;y<=last;y++){ if(s[x]>s[y]){ tmp=s[x]; s[x]=s[y]; s[y]=tmp; } } } t[i]=s[first+2]; } pivotitem=select(r,t,(r+1)/2); j=low; for(i=low;i<=high;i++) if(s[i]==pivotitem){ tmp=s[i]; s[i]=s[j]; s[j]=tmp; mark=j; j++; } else if(s[i]<pivotitem){ tmp=s[i]; s[i]=s[j]; s[j]=tmp; j++; } *pivotpoint=j-1; tmp=s[mark]; s[mark]=s[j-1]; s[j-1]=tmp; } int selection2(int *s,int low,int high,int k) { int pivotpoint; if(high==low) return s[low]; else{ partition2(s,low,high,&pivotpoint); if(k==pivotpoint) return s[pivotpoint]; else if(k<pivotpoint) return selection2(s,low, pivotpoint-1,k);

  • プログラミング構造体について。

    include<stdio.h> #include<stdlib.h> #include<string.h> struct person{ char name[10]; int gender; int age; }; void printPersonList(struct person *person_p, int size); void outputPersonList(struct person *person_p, int size); double getAverageOfAge(struct person *person_p, int size); int countMales(struct person *person_p, int size); int countFemales(struct person *person_p, int size); int main(void){ struct person *person_p; int i, count, gender, age, maleCount, femaleCount; char name[20]; double average; printf("登録する人数を入力してください。\n"); scanf("%d", &count); person_p = (struct person*)malloc(sizeof(struct person)* count); for(i=0; i < count; i++){ printf("名前・性別(男性:0, 女性:1)・年齢をスペース区切りで入力してください。\n"); scanf("%s %d %d", name, &gender, &age); strcpy((person_p + i) -> name, name); (person_p + i) -> gender = gender; (person_p + i) -> age = age; } printPersonList(person_p, count); outputPersonList(person_p, count); average = getAverageOfAge(person_p, count); printf("平均年齢:%f\n", average); maleCount = countMales(person_p, count); femaleCount = countFemales(person_p, count); printf("男性:%d名, 女性:%d名\n", maleCount, femaleCount); free(person_p); return 1; } void printPersonList(struct person *person_p, int size){ int i; printf("登録リスト\n"); printf(" name | gender | age\n"); printf("----------+--------+-----\n"); for(i=0; i < size; i++){ printf("%10s | %1d | %2d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } } void outputPersonList(struct person *person_p, int size){ FILE *output; int i; if((output = fopen("meibo.c", "w")) == NULL){ printf("meibo.cを開けませんでした。\n"); return; } for(i=0; i < size; i++){ fprintf(output, "%s, %d, %d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } fclose(output); } 残り3つの関数をすべて定義する(それぞれ10行程度) getAverageOfAge, countMales, countFemales どう定義すればいいのか教えてください。お願いします。