全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:749
推到 Plurk!
推到 Facebook!

程式問題

尚未結案
woods
一般會員


發表:11
回覆:8
積分:3
註冊:2003-10-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-24 21:23:15 IP:140.130.xxx.xxx 未訂閱
能幫我看看  是那裡出錯了嗎??    compile後出現    g++ -c main3.cpp -o main3.o    main3.cpp: In function `void readupperData (basic_string, __default_alloc_template >, vector > *)': main3.cpp:58: `f1' undeclared (first use this function) main3.cpp:58: (Each undeclared identifier is reported only once for each function it appears in.) main3.cpp: In function `void readdownData (basic_string, __default_alloc_template >, vector > *)': main3.cpp:87: `f2' undeclared (first use this function)
#include 
#include 
#include 
#include 
#include <math.h>
#include 
#include 
#include 
#include 
#include 
#include 
#include     void readupperData(string, vector*);
void readdownData(string, vector*);    int main (int argc, char ** argv){
std::vector x;
std::vector y;
std::string a;
std::string b;    for(int i = 0; i < argc; i  ){
if ((strcmp(argv[i], "f1.txt") == 0) && (i != argc - 1)){
a = argv[i 1];
break;
}
}    if ( a == "f1.txt"){
cout <<"Please give a filename for tsplib data by including the pair"
<< " \"file \" on the command line.\n";
exit(0);
}    for(int i = 0; i < argc; i  ){
if ((strcmp(argv[i], "f2.txt") == 0) && (i != argc - 1)){
b = argv[i 1];
break;
}
}
if ( b == "f2.txt"){
cout <<"Please give a filename for tsplib data by including the pair"
<< " \"file \" on the command line.\n";
exit(0);
}
}        void readupperData (string a, vector* x){
ifstream in(a.c_str()); 
int towns = 0;
double temp; /* A temp variable for input */
if(!in){
cerr << "Cannot open input file: " << f1 << std::endl;
exit(1);
}
do {
in >> temp;
if (!in.eof()){
in >> temp;
x->push_back(temp);
towns  ;
}
} while (!in.eof());
in.close();    for(int i = 0; i < x->end() - x->begin(); i  ){
std::cout << (i 1) << " " << (*x)[i] << endl;
}    for (int i = 0; i < towns; i  ){
cout << "x is " << (*x)[i] << std::endl;
}
}    void readdownData (string b,vector* y){
ifstream in(b.c_str()); /* Open our tsp data file */
int towns = 0;
double temp; /* A temp variable for input */
if(!in){
cerr << "Cannot open input file: " << f2 << std::endl;
exit(1);
}
do {
in >> temp;
if (!in.eof()){
in >> temp;
y->push_back(temp);
towns  ;
}
} while (!in.eof());
in.close();    for(int i = 0; i < y->end() - y->begin(); i  ){
std::cout << (i 1) << " " << (*y)[i] << endl;
}    for (int i = 0; i < towns; i  ){
cout <<" y is " << (*y)[i] << std::endl;
}    }     
mieng
中階會員


發表:12
回覆:97
積分:81
註冊:2003-10-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-24 22:32:46 IP:61.221.xxx.xxx 未訂閱
sorry~~偶只看出您的 f1 和 f2 未定義的錯誤,    把下面兩行寫在.h檔裡試試
void readupperData(string, vector*);
void readdownData(string, vector*);
學習中請大家多多指導呦~~ ☆星霧☆(Mineg Chien WU)
woods
一般會員


發表:11
回覆:8
積分:3
註冊:2003-10-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-25 14:01:43 IP:140.130.xxx.xxx 未訂閱
我後來可以compile過了 但是執行時會有錯誤 能幫我看看那裡有邏輯上的錯誤嗎    錯誤都會無法讀入檔案 Cannot open input file:     
#include 
#include 
#include 
#include 
#include <math.h>
#include 
#include 
#include 
#include 
#include 
#include 
#include     void readupperData(string, vector*);
void readdownData(string, vector*);    int main (int argc, char ** argv){
std::vector x;
std::vector y;
std::string a;
std::string b;    for(int i = 0; i < argc; i  )
{
if ((strcmp(argv[i], "f1.txt") == 0) && (i != argc - 1))
{
a = argv[i 1];
break;
}
}    if ( a == "f1.txt")
{
cout <<"Please give a filename for tsplib data by including the pair"
<< " \"file \" on the command line.\n";
exit(0);
}    readupperData(a, &x);
int t1 = x.end() - x.begin();    for(int i = 0; i < argc; i  )
{
if ((strcmp(argv[i], "f2.txt") == 0) && (i != argc - 1))
{
b = argv[i 1];
break;
}
}
if ( b == "f2.txt")
{
cout <<"Please give a filename for tsplib data by including the pair"
<< " \"file \" on the command line.\n";
exit(0);
}
readdownData(b, &y);
int t2 = y.end() - y.begin();
}    void readupperData (string a, vector* x){
ifstream in(a.c_str()); 
int t1 = 0;
double temp; /* A temp variable for input */
if(!in){
cerr << "Cannot open input file: " << a << std::endl;
exit(1);
}
do {
in >> temp;
if (!in.eof()){
in >> temp;
x->push_back(temp);
t1  ;
}
} while (!in.eof());
in.close();    for(int i = 0; i < x->end() - x->begin(); i  ){
std::cout << (i 1) << " " << (*x)[i] << endl;
}    for (int i = 0; i < t1; i  ){
cout << "x is " << (*x)[i] << std::endl;
}
}    void readdownData (string b,vector* y){
ifstream in(b.c_str()); 
int t2 = 0;
double temp; /* A temp variable for input */
if(!in){
cerr << "Cannot open input file: " << b << std::endl;
exit(1);
}
do {
in >> temp;
if (!in.eof()){
in >> temp;
y->push_back(temp);
t2  ;
}
} while (!in.eof());
in.close();    for(int i = 0; i < y->end() - y->begin(); i  ){
std::cout << (i 1) << " " << (*y)[i] << endl;
}    for (int i = 0; i < t2; i  ){
cout <<" y is " << (*y)[i] << std::endl;
}    }
 
woods
一般會員


發表:11
回覆:8
積分:3
註冊:2003-10-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-11-25 16:14:26 IP:140.130.xxx.xxx 未訂閱
幫幫我吧
mieng
中階會員


發表:12
回覆:97
積分:81
註冊:2003-10-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-11-25 17:23:34 IP:61.60.xxx.xxx 未訂閱
無法讀入檔案,通常都是檔案路徑錯誤或是檔案不存在造成的,您可以先從這方面檢查看看~其他的我的經驗不足無法幫忙囉~=..=~ 學習中請大家多多指導呦~~ ☆星霧☆(Mineg Chien WU)
mieng
中階會員


發表:12
回覆:97
積分:81
註冊:2003-10-31

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-11-27 17:17:25 IP:61.60.xxx.xxx 未訂閱
當您的程式跑到 ifstream in(a.c_str()); 這時,因為a為空字串,所以無法開啟檔案,b的情形相同
for(int i = 0; i < argc; i  )
{
  if ((strcmp(argv[i], "f1.txt") == 0) && (i != argc - 1))
  {
    a = argv[i 1];
    break;
  }
}
 
上面這段程式因為argv內容為程式名,因此不會進到if內指定a的內容,所以您的a為空字串, 當程式跑到 readupperData(a, &x); 函式時,因為您的a為空字串,故無法開啟檔案,所以需要自行指定a的內容,或由使用者輸入,但不幸的是你加了下面這段程式,造成您指定a的內容為"f1.txt"後,就直接離開了,所以在最下面的程式中我把這兩段程式都拿掉了,如果你要把下面這段程式加進去的話,我想改成 if(a != "f1.txt") 應該才對。
if ( a == "f1.txt")
{
  cout <<"Please give a filename for tsplib data by including the pair"
<< " \"file \" on the command line.\n";
  exit(0);
}
試試下面的程式是否是您所需要的
#include 
#include 
#include 
#include 
#include <math.h>
#include 
#include 
#include 
#include 
#include 
#include 
#include     void readupperData(string, vector*);
void readdownData(string, vector*);    int main (int argc, char ** argv)
{
  std::vector x;
  std::vector y;
  std::string a;
  std::string b;
//直接指定a和b的內容
  a="f1.txt";
  b="f2.txt";
//或另行輸入
//  cin >> a;
//  cin >> b;      readupperData(a, &x);      int t1 = x.end() - x.begin();
  cout << "t2=  " << t1 << endl;      readdownData(b, &y);
  int t2 = y.end() - y.begin();
  cout << "t2=  " << t2 << endl;
}    void readupperData (string a, vector* x)
{
  ifstream in(a.c_str());
  int t1 = 0;
  double temp; /* A temp variable for input */
  if(!in)
  {
    cerr << "Cannot open input file: " << a << std::endl;
    exit(1);
  }
  do
  {
    in >> temp;
      if (!in.eof())
      {
        in >> temp;
        x->push_back(temp);
        t1  ;
    }
  } while (!in.eof());
  in.close();      for(int i = 0; i < x->end() - x->begin(); i  )
  {
    std::cout << (i 1) << " " << (*x)[i] << endl;
  }      for (int i = 0; i < t1; i  ){
    cout << "x is " << (*x)[i] << std::endl;
  }
}    void readdownData (string b,vector* y)
{
  ifstream in(b.c_str());
  int t2 = 0;
  double temp; /* A temp variable for input */
  if(!in)
  {
    cerr << "Cannot open input file: " << b << std::endl;
    exit(1);
  }
  do
  {
    in >> temp;
    if (!in.eof())
    {
      in >> temp;
      y->push_back(temp);
      t2  ;
    }
  } while (!in.eof());
  in.close();      for(int i = 0; i < y->end() - y->begin(); i  )
  {
    std::cout << (i 1) << " " << (*y)[i] << endl;
  }      for (int i = 0; i < t2; i  )
  {
    cout <<" y is " << (*y)[i] << std::endl;
  }
}
 
PS:第一次玩dos下的程式~不太習慣~=..=~ 學習中請大家多多指導呦~~ ☆星霧☆(Mineg Chien WU) 發表人 - mieng 於 2003/11/27 17:24:33
woods
一般會員


發表:11
回覆:8
積分:3
註冊:2003-10-14

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-11-27 17:30:48 IP:140.130.xxx.xxx 未訂閱
太感謝你了 我感動的痛哭流涕 謝謝
系統時間:2024-07-02 2:42:08
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!