2013-08-12 3 views
1

저는 이것이 뼈가 단순하지만 간단히 볼 수는 없다고 확신합니다. C++ Xcode에서 다음과 같은 링커 오류가 발생합니다.정적 메서드에 대한 Xcode의 C++ 링커 오류

Undefined symbols for architecture x86_64: 
"Random::NextInt(int, int)", referenced from: 
Helpers::MakeData(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int) in Helpers.o 

도우미 :: MakeData

#include "Helpers.h" 
#include "Random.h" 

void Helpers::MakeData(string dataFile, int numLines) 
{ 
    vector<double> weights = { -0.1, 0.2, -0.3, 0.4, -0.5, 
    0.6, -0.7, 0.8, -0.9, 1.0, 
    -1.1, 1.2, -1.3, 1.4, -1.5, 
    1.6, -1.7, 1.8, -1.9, 2.0, 
    -0.5, 0.6, -0.7, 0.8, -0.9, 
    1.5, -1.4, 1.3, 
    -1.2, 1.1, -1.0, 
    0.9, -0.8, 0.7, 
    -0.6, 0.5, -0.4, 
    0.3, -0.2, 0.1, 
    0.1, -0.3, 0.6 }; 

    NeuralNetwork * nn = new NeuralNetwork(4, 5, 3); 
    nn->SetWeights(weights); 
    ofstream myFile; 
    myFile.open(dataFile); 
    for (int i = 0; i < numLines; ++i) 
    { 
     vector<double> inputs; 
     for (int j = 0; j < inputs.size(); ++j) 
      inputs[j] = Random::NextInt(10, 1); 

     vector<double> outputs = nn->ComputeOutputs(inputs); 

     string color = ""; 
     int idx = Helpers::IndexOfLargest(outputs); 
     if (idx == 0) { color = "red"; } 
     else if (idx == 1) { color = "green"; } 
     else if (idx == 2) { color = "blue"; } 

     myFile << inputs[0] << " " << inputs[1] << " " << inputs[2] << " " << inputs[3] <<  " " << color; 
    } 
    myFile.close(); 
} // MakeData 

Random.h

#ifndef __NeuralClassificationProgram__Random__ 
#define __NeuralClassificationProgram__Random__ 

#include <iostream> 
class Random{ 

public: 
    static double NextDouble(); 
    static int NextInt(int high, int low); 
}; 

#endif /* defined(__NeuralClassificationProgram__Random__) */ 

Random.cpp

#include "Random.h" 
#include <time.h> 
#include <stdlib.h> 

double NextDouble() 
{ 
    double rnd; 
    srand(static_cast<unsigned int>(time(NULL))); 
    rnd = rand() % 1+0; 
    return rnd; 
} 

int NextInt(int high, int low) 
{ 
    int rnd; 
    srand(static_cast<unsigned int>(time(NULL))); 
    rnd = rand() % high + low; 
    return rnd; 
} 
+0

'Random :: NextDouble'과'Random :: NextInt (int high, int low)'가'Random.cpp'에 필요합니다. – lcs

답변

1

그것은 당신이 Random::NextInt(int, int)을 정의하지 않기 때문에, 당신은 NextInt(int, int)을 정의합니다.

즉, 클래스 범위 연산자를 잊었습니다.

int Random::NextInt(int high, int low) 
{ 
    return rand() % high + low; 
} 

아를 시도하고 이 프로그램에서 두 번 이상하지 전화 srand 더 많은 일을 할.

+0

고마워요 ... 그게 날 짜증나게했습니다. –

1
당신은 구현 클래스 이름 규정을 포함하지 않았다

, 그래서 그 "정적 방법"은 간단한 전역 함수로 컴파일됩니다.

예를 들어, 당신은 :

int NextInt(int high, int low) 

그러나 당신이 필요합니다

int Random::NextInt(int high, int low) 
+0

고마워요. 그게 날 짜증나게했습니다. –