Compile Errors, need help

I got the below compile errors. Any idea what could be wrong here?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>
#include <vector>

class MergeNames
{
public:
    static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
    {
		//throw std::logic_error("Waiting to be implemented");
        std::vector<std::string> vector_unique;
		vector_unique.insert(vector_unique.end(), names1.begin(), names1.end());
		vector_unique.insert(vector_unique.end(), names2.begin(), names2.end());
		std::sort(vector_unique.begin(), vector_unique.end());
		auto last = std::unique(vector_unique.begin(), vector_unique.end());
		vector_unique.erase(last, vector_unique.end());
        return vector_unique;
    }
};

#ifndef RunTests
int main()
{
    std::vector<std::string> names1 = {"Ava", "Emma", "Olivia"};
    std::vector<std::string> names2 = {"Olivia", "Sophia", "Emma"};
    
    std::vector<std::string> result = MergeNames::unique_names(names1, names2);
    for(auto element : result)
    {
        std::cout << element << ' '; // should print Ava Emma Olivia Sophia
    }
}
#endif











Error running solution, see below


Compiler error output:
Main.cpp:13:
./ProblemTest.cpp:19:43: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
std::vector<std::string> actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:39:43: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
std::vector<std::string> actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:50:18: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:70:43: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
std::vector<std::string> actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:81:18: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:101:43: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
std::vector<std::string> actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
Main.cpp:13:
./ProblemTest.cpp:112:18: error: use of undeclared identifier 'unique_names'; did you mean 'MergeNames::unique_names'?
actual = unique_names(vector1, vector2);
^~~~~~~~~~~~
MergeNames::unique_names
./Problem.cpp:17:37: note: 'MergeNames::unique_names' declared here
static std::vector<std::string> unique_names(const std::vector<std::string>& names1, const std::vector<std::string>& names2)
^
7 errors generated.
Error mentions
std::vector<std::string> actual = unique_names(vector1, vector2);

but your code does not have such statement. The errors are not from the posted code.
Runs fine in cpp.sh

You have a version-control problem.
Compiles and runs OK with VS.

Is this an exercise? Is unique_names() for insert called many times before actual std::vector is used? Or is the vector accessed before all insertions are done? How many elements are expected to be stored in the vector?

If insert is called many times before the vector is used, then a possible improvement would be to insert all the elements first and then de-duplicate the ve4ctor after finished inserting.

Which is 'best' depends upon the circumstances as to how the elements are added to the vector and how/when the vector is used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

class MergeNames
{
public:
    static std::vector<std::string> unique_names( const std::vector<std::string>& names1, const std::vector<std::string>& names2 )
    {
        std::set<std::string> S( names1.begin(), names1.end() );
	S.insert( names2.begin(), names2.end() );
        return std::vector<std::string>( S.begin(), S.end() );
    }
};

#ifndef RunTests
int main()
{
    std::vector<std::string> names1 = {"Ava", "Emma", "Olivia"};
    std::vector<std::string> names2 = {"Olivia", "Sophia", "Emma"};
    
    std::vector<std::string> result = MergeNames::unique_names(names1, names2);
    for(auto element : result) std::cout << element << ' ';
}
#endif 
Last edited on
For adding all the elements first and then de-duplicate, possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>

void unique_names(std::vector<std::string>& names)
{
	const std::set<std::string> S(names.begin(), names.end());

	names.clear();
	names.insert(names.end(), S.begin(), S.end());
}

int main()
{
	const std::vector<std::string> names1 {"Ava", "Emma", "Olivia"};
	const std::vector<std::string> names2 {"Olivia", "Sophia", "Emma"};

	std::vector<std::string> result;

	result.insert(result.end(), names1.begin(), names1.end());
	result.insert(result.end(), names2.begin(), names2.end());

	unique_names(result);

	for (const auto& element : result)
		std::cout << element << ' '; // should print Ava Emma Olivia Sophia

	std::cout << '\n';
}



Ava Emma Olivia Sophia

Last edited on
Thanks everyone for quick help and suggestions.
Topic archived. No new replies allowed.