Verstehe Compilerfehlermeldung nicht - Grundsätzlicher Programmaufbau



  • Hallo zusammen,

    ich habe mir vor kurzem einige c++ Kenntnisse angeeignet und jetzt probiert ein klein wenig größeres Programm zu schreiben.
    Das Programm soll Proteinfaltungen auf einem 2D Raster durchführen und dabei nach Konfiguratioen fahnden bei denen möglichst viele hydrophobe Elemente aus einer Aminosäure aneinanderkleben. Weitere Infos dazu stehen im Kommentar.

    Meine Fragen dabei sind:
    -Ist der grundsätzliche Aufbau des Programms so gut: Von der main Methode die anderen Methoden aufrufen.
    -Das Programm läuft bei weitem noch nicht ohne Compiler fehler - leider verstehe ich die Hinweise nicht. Vielleicht kann mir jemand meine größten (häufigsten Fehler) erklären. Das wäre spitze.
    -Brauche ich die Klassendefinition am Anfang? Falls nein: Wie kann ich ansonsten public variablen deklarieren?

    Des Weiteren:
    -Ich habe das ganze in Eclipse geschrieben und benütze als compiler den mingw.
    -Weitere kleinere Programme habe ich schon erfolgreich geschrieben, compiliert und ausgeführt --> an der Infrastruktur kanns nicht liegen

    Vielen Dank schon mal für Hinweise aller Art 🙂

    #include <iostream>
    #include <iomanip>
    #include <stdio.h>
    #include <fstream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    using namespace std;
    
    /*
    This program tries to find a possibly low engergystate for a chain of amino acids.
    Thereby a pair of hydophobic acids (but non-adjacent in the chain) result in a decrease in Energy by one.
    To get this job done this program performs a simulated annealing.
    Within this method temperature will be decreased slowly. With decreasing temperature the uphill probability will decrease.
    Thus the search for the minimum will change from a random search to a local search.
    
    In the 'Main-Loop' in the main function the temperature decrease will be performed and at each temperature the function Proteinpivot will be called repeatedly until a self avoiding walk has been found.
    A self avoiding walk is a change in the way the aminoacidchain is arranged on a 2D lattice.
    The function Proteinpivot performs changes of such arrangements around a pivot element. This element and the way it is moved will be chosen randomly.
    The function count_contacts counts the number of hydophobic elements being located next to each other (not considering pairs of hydrophobic elements in the chain).
    
    In the program these variables will be used
    
    int N (number of amino acids)
    int seq[N] (vector holding the amino acid sequence (1 for H, and 0 for P)
    int x[N] (x position for the aa sequence)
    int xf[N] temporary x[N]
    int y[N] (y position for the aa sequence)
    int yf[N] temporary y[N]
    int occ[L][L] (flag for when a lattice site is occupied by an amino acid, i+1=aa i at pos and 0=no)
    int L is the lattice size and should be set to 2N+1
    int nhh,nhp,npp (number of contacts of the different forms)
    double beta (one over temperature, 1/T)
    int ret (found a self avoiding walk-->1, 0 else)
    int oldE (storing the Energy of the old configuration)
    int newE (storing the Energy of the new configuration)
    
    */
    
    class Protein {
    public:
       int oldE;
       int newE;
       int N; //(number of amino acids)
       int seq[N]; //(vector holding the amino acid sequence (1 for H, and 0 for P)
       int x[N]; //(x position for the aa sequence)
       int y[N]; //(y position for the aa sequence)
       int xf[N];
       int yf[N];
       int L; //is the lattice size and should be set to 2N+1
       int occ[L][L]; //(flag for when a lattice site is occupied by an amino acid, i+1=aa i at pos and 0=no)
       int nhh,nhp,npp;
       double beta;//(number of contacts of the different forms)
    
       int ret;
    //private:
    };
    
    int main () {
    	int i,j;
    	// Input variables
    	   int N=6; //(number of amino acids)
    	   int seq[N]; //(vector holding the amino acid sequence (1 for H, and 0 for P)
    	   seq[0]=0;
    	   seq[1]=1;
    	   seq[2]=1;
    	   seq[3]=1;
    	   seq[4]=1;
    	   seq[5]=0;
    	   int x[N]; //(x position for the aa sequence)
    	   int y[N]; //(y position for the aa sequence)
    	   for (i=0;i<6;i++){
    		   x[i]=5;
    		   y[i]=5+i;
    	   }
    	   int L=13; //is the lattice size and should be set to 2N+1
    	   int occ[L][L]; //(flag for when a lattice site is occupied by an amino acid, i+1=aa i at pos and 0=no)
    	   for (i=0;i<L;i++) {
    		   for (j=0;j<L;j++) {
    			   occ[i][j]=0;
    			   if (i==5) {
    				   if (4<j && j<13) {
    					   occ[i][j]=j-4;
    				   }
    			   }
    		   }
    	   }
    	   int nhh,nhp,npp;
    	   nhh=N/2;
    	   nhp=0;
    	   npp=0;
    	   double beta;
    	   beta=1/5000;
    
    //Main-loop
    	   int oldE=N/2;
    	   while (beta<0.1){ //not yet cold enough
    
    	   	   oldE=count_contacts(N,seq,x,y,occ);
    	   	   int ret=0;
    	   	   while (ret==0){ //while: could not successfully transform sequence because of self crossing
    	   		  Proteinpivot(N,seq,x,y,occ,beta,oldE);
    	   	   }
    
    	   	   //increase beta
    	   	   double T=1/beta;
    	   	   T=T-1;//increase Temp. by 1 unit
    	   	   beta=1/T;
    	      }
    
    // --- Save last state to file ---
    	      char filename[8]="out.dat";
    	      ofstream outputfile(filename);
    	      // Save the different heights and the corresponding densities
    	      for (int n=0; n<=N-1; n++){
    	          //outputfile<<setw(16)<<nhh<<setw(16)<<x[n]<<setw(16)<<y[n]<<endl;
    	    	  outputfile<<nhh;
    	      }
    	      outputfile.close();
    	      cout<<"Output data saved in out.dat."<<endl;
    
    return 0;
    }
    
    void Proteinpivot(int N, int seq[], int x[], int y[], int occ[][], double beta, int oldE)
    {
      int xf[N], yf[N];
      int i, j, k, symm_op, tmp, hh, hp, pp;
      int ret;
    
      //Generate new conformation
    
      time_t t;
      time(&t);
      srand((unsigned int)t);              /* initialize random number generator */
    
      k = (rand() % N+1)-1; // Random point to pivot around
      symm_op = (rand() % 7+1)-1;  // Type of pivot move (randomly selected)
      // Calculate coordinates relative to pivot point
      for(i = k + 1; i < N; i++) // i++ -> i=i+1
      {
        xf[i] = x[i] - x[k];
        yf[i] = y[i] - y[k];
      }
    
      switch(symm_op)
      {
      case 0:  // Rotate 90 degrees
        for(i = k + 1; i < N; i++)
        {
          tmp = xf[i];
          xf[i] = -yf[i];
          yf[i] = tmp;
        }
        break;
      case 1:  // Rotate -90 degrees
        for(i = k + 1; i < N; i++)
        {
          tmp = xf[i];
          xf[i] = yf[i];
          yf[i] = -tmp;
        }
        break;
      case 2:  // Rotate 180 degrees
        for(i = k + 1; i < N; i++)
        {
          xf[i] = -xf[i];
          yf[i] = -yf[i];
        }
        break;
      case 3:  // Mirror (1,0)
        for(i = k + 1; i < N; i++)
        {
          yf[i] = -yf[i];
        }
        break;
      case 4:  // Mirror (0,1)
        for(i = k + 1; i < N; i++)
        {
          xf[i] = -xf[i];
        }
        break;
      case 5:  // Mirror (1,1)
        for(i = k + 1; i < N; i++)
        {
          tmp = xf[i];
          xf[i] = yf[i];
          yf[i] = tmp;
        }
        break;
      case 6:  // Mirror (1,-1)
        for(i = k + 1; i < N; i++)
        {
          tmp = xf[i];
          xf[i] = -yf[i];
          yf[i] = -tmp;
        }
        break;
      }
    
      // Move the origin back
      for(i = k + 1; i < N; i++)
      {
        xf[i] += x[k]; // xf[i] = xf[i] + x[k];
        yf[i] += y[k];
      }
      for(i = 0; i <= k; i++)
      {
        xf[i] = x[i];
        yf[i] = y[i];
      }
    
      int ret=0; // not sure whether is needed??????????????????????????
      // New conformation ready - now check if it's a SAW
      for(i = k + 1; i < N; i++)
        occ[x[i]][y[i]] = 0;
    
      for(i = k + 1; i < N; i++)
      {
        if(occ[xf[i]][yf[i]] > 0)
        {
          for(j = k + 1; j < i; j++)
            occ[xf[j]][yf[j]] = 0;
          for(j = k + 1; j < N; j++)
            occ[x[j]][y[j]] = j + 1;
          ret=0;
          //return 0;
        }
        else
          occ[xf[i]][yf[i]] = i+1;
        	ret=1;
        	//return 1;
      }
    
      if (ret!=0) {
    	  // OK, it's a SAW. Accept or reject it?
    	  // METROPOLIS
    	  //??? better use n in front????
    	  int newE=N/2;
    	  newE=count_contacts(N,seq,x,y,occ);
    
    	  int p;
    	  double h=1;
    	  p=min(h,exp(-(newE-oldE)*beta));
    
    	  time(&t);
    	  srand((unsigned int)t);
    	  double random_number = rand() / ((double)RAND_MAX + 1);
    
    	  if (random_number<=p){//ACCEPT"!
    		  // - updating the old into new state OR
    
    		  for (i=0;i<N,i++) {
    			  x[i]=xf[i];
    			  y[i]=yf[i];
    			  // -and resetting the old
    			  //do i need to reset xf und yf , too???????????????????????????????????????????????????
    		  }
    
    	  }
    	  else {//Reject
    		  //occ reset
    		  for (i=k+1;i<N,i++) {
    			  occ[xf[i]][yf[i]]=0;
    			  occ[x[i]][y[i]]=i+1;
    		  }
    
    	  }
      }
    
    }
    
    /* Function for calculating contacts among amino acids NOT neighbors in the chain.
       Used to calculate an energy
    */
    int count_contacts(int N, int seq[], int x[], int y[],int occ[][]) // ???? better n in front???????????
    {
      int i, j, k;
    
      // nhh =nhp = npp = 0;
      int nhh=0;
      for(i = 0; i < N; i++)
      {
        for(j = 0; j < 4; j++)
        {
          k = occ[x[i] + (j == 0 ? 1 : (j == 2 ? -1 : 0))] //j==0 -> (x+1,y), j==2 -> (x-1,y)
            [y[i] + (j == 1 ? 1 : (j == 3 ? -1 : 0))] - 1; //j==1 -> (x,y+1), j==3 -> (x,y-1)
          if(k > i + 1)
          {
            if(!seq[i] && !seq[k])
              //(npp)++;
            else if(seq[i] && seq[k])
              (nhh)++;
            else
              //(nhp)++;
          }
        }
      }
      return nhh;
    }
    


  • Erster Fehler: Der Code ist zu viel und du hast vergessen die Fehlermeldungen mitzugeben.
    Zweiter Fehler: Bei Arrays muß zur Compilezeit bekannt sein, wieviele Elemente sie haben - dadurch sind die meisten Elemente der "Klasse" Protein syntaktisch falsch.

    PS: als "Methode" bezeichnet man in C++ nur die Memberfunktionen einer Klasse - main() und Proteinpivot() sind normale Funktionen 😉


Anmelden zum Antworten