문제

For some reason, even though I have a main() function defined, I am receiving a LNK1561 error.

My search.h file:

#pragma once

#include <iostream>
#include <fstream>

using namespace std;

namespace standard{
class Search
{
public:
    bool sequential_find(int num);
    bool recursive_binary_find(int num);
    bool iterative_binary_find(int num);

    void init_array();
    void init_sorted_array();
    void set_seed(int seed);
    int getSize();

    bool binSearch(int d, int low, int high);

    // this is just for practice and debugging.
    friend ostream& operator<< (ostream& out, const Search& s)
    {
        // put the code in here.
    }

    Search(int size,int seed=0);
    ~Search();

private:

    int size;
    int* array;
    static int* data;
};
}

And my search.cpp file:

#include "Search.h"

    using namespace std;

    namespace standard{

        int main()
        {
            return 0;
        }

    //  Search::Search(int size,int seed=0)
    //  {
    //      
    //  }

        bool Search::sequential_find(int num)
        {
            for (int i=0; i<getSize(); i++)
            {
                if(data[i] == num)
                {
                    return true;
                }
            }
            return false;
        }


        bool Search::recursive_binary_find(int num)
        {
            int high = getSize()-1;
            int low = 0;
            return binSearch(num, low, high);
        }

        bool Search::binSearch(int d, int low, int high)
        {
            if (low > high)
                return false;
            int mid = (high+low)/2;
            if (d == data[mid])
                return true;
            if (d < data[mid])
                return binSearch(d, low, mid-1);
            else
                return binSearch(d, mid+1, high);
        }

        bool Search::iterative_binary_find(int num)
        {
            int high = getSize()-1;
            int low = 0;
            while (low <= high)
            {
                int mid = (high+low)/2;
                if (num == data[mid])
                    return true;
                else if (num < data[mid])
                    high = mid-1;
                else if (num > data[mid])
                    low = mid+1;
            }
            return false;
        }

        void Search::init_array()
        {

        }
        void Search::init_sorted_array()
        {

        }
        void Search::set_seed(int seed)
        {

        }
        int Search::getSize()
        {
            return size;
        }
    }

Obviously I haven't done anything substantial in my main function, I am working on just getting it to compile at all.

도움이 되었습니까?

해결책

You have put your main() function in the standard namespace. That's not the entry point of your program. The entry point of your program should be in the global namespace.

Per paragraph 3.6.1/1 of the C++11 Standard:

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.

To fix the problem, just move your main() to the global namespace. Also notice, that return 0 is not necessary. Flowing off the end of main() is equivalent to returning 0.

다른 팁

You put main inside your own namespace standard, which makes it a function belongs to your namespace, not a global function which main should be anymore.

For C++ standard 3.6.1:

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [...]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top